Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/react-component-ref/src/RefFindNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ export default class RefFindNode extends React.Component<RefFindNodeProps> {
handleRef(this.props.innerRef, this.prevNode)
}

componentDidUpdate() {
componentDidUpdate(prevProps: RefFindNodeProps) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think that it might be beneficial to implement this component with Hooks, for couple of reasons

  • in that case we will have explicit update dependencies specified in useEffect
  • explicitness on unsubscribe actions for effects
  • this component is fairly small in terms of amount of code

const currentNode = ReactDOM.findDOMNode(this)

if (this.prevNode !== currentNode) {
this.prevNode = currentNode
handleRef(this.props.innerRef, currentNode)
}

if (prevProps.innerRef !== this.props.innerRef) {
handleRef(this.props.innerRef, currentNode)
}
}

componentWillUnmount() {
Expand Down
19 changes: 19 additions & 0 deletions packages/react-component-ref/test/RefFindNode-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,24 @@ describe('RefFindNode', () => {
expect(innerRef).toHaveBeenCalledTimes(1)
expect(innerRef).toHaveBeenCalledWith(expect.objectContaining({ tagName: 'DIV' }))
})

it('handles updates of props', () => {
const initialRef = jest.fn()
const updatedRef = jest.fn()
const wrapper = mount(
<RefFindNode innerRef={initialRef}>
<div />
</RefFindNode>,
)

expect(initialRef).toHaveBeenCalled()
expect(updatedRef).not.toHaveBeenCalled()

jest.resetAllMocks()
wrapper.setProps({ innerRef: updatedRef })

expect(initialRef).not.toHaveBeenCalled()
expect(updatedRef).toHaveBeenCalled()
})
})
})