- Notifications
You must be signed in to change notification settings - Fork 733
docs: update the rerender method using the unmount method before #1015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point of rerender is to just update the old UI. If you need a fresh mount, you should just use render
instead. In almost all scenarios where you want to rerender, you don't want to unmount.
Could you elaborate why you need to unmount+rerender instead of just render?
I had a problem with the following scenario: I have a component that renders some items according to the props it receives. And to test if the items are being dynamically rendered according to the props, I rendered the component passing the X props and soon after I passed my component using rerender with the Y props. Here's an example: |
Let's discuss this with the concrete implementation of the componen available in the original issue. Otherwise it's not clear what you're trying to do |
right, so let's discuss based on the following example that is presented in the documentation: import {render} from '@testing-library/react' const {rerender} = render(<NumberDisplay number={1} />) // re-render the same component with different props rerender(<NumberDisplay number={2} />) In the first declaration of the component we expect for test cases that the props number returns 1. So far so good. . After the rerender, we expect it to return 2, but the component will still carry the reference of the first declaration which is 1. That's why I used the unmount() method so that from the rerender declaration it returns 2. |
Rerender with different prop value: |
Perfect! But if we don't use the "screen" it keeps pointing to the reference of the first component. |
I don't understand what you are referring to. Could you please elaborate? |
From the description of the component that inspired the issue, function List({initialItems}) { const [items, setItems] = useState(initialItems) // starting value useEffect(() => { setItems(initialItems) // update the component }, [initialItems]) return ( <ul>{items.map(item => <li>{item}</li>)}</ul> ) } |
But it is the same DOM reference.. As long as the same element is rendered only with different attributes, React will make the changes on the actual DOM instance and not create a new one.. |
The rerender method is a method that needs to be unmounted, otherwise it will take the previous rendering context, thus failing the test.
I recently ran into this problem and didn't find it in the "rerender" method section in the documentation.