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
Cant set the props or state of a component inside MemoryRouter #1384
Comments
|
@ljharb thanks Jordan but i get this error using that line of code: setProps() can only be called on the root :/ any other approaches i can try ? |
ah, yes. #361 (which i see you commented on) is indeed relevant. The solution here tho, is that you'd instead want |
does children refer to the Login component ? and also i'm unclear with what is the replacementLoginComponent meant to be ? |
@WalidKurchied Try this code: wrapper.setProps({
children: cloneElement(wrapper.props().children, { agent: agent }),
}); |
What about the state? Since you can only set the state on root and root is now MemoryRouter ? |
Use shallow for that. |
So if I need to change a prop on a component nested two HOCs deep, I need to do something like clone and set children twice? Are there plans to make this API easier to use? |
I am having a problem with this too. We have multiple levels of HOCs deep. Can anyone help us come up with a better solution than this?
|
So @enniel's approach mostly worked (thanks!), but with some strange behavior, possibly just because I don't understand fully. Looking at function WrapNavBar_TriggerComponentDidUpdate_ReturnNavBar(displaySavedIndicator: boolean) {
const wrapper = mount(wrap(<NavigationBar {...navigationBarProps} />));
wrapper.setProps({
children: React.cloneElement(wrapper.props().children.props.children, { displaySavedIndicator: false }),
});
wrapper.setProps({
children: React.cloneElement(wrapper.props().children, { displaySavedIndicator }),
});
wrapper.update();
return wrapper.find(NavigationBar);
} In the above code, |
I have a Login component thats inside a MemoryRouter component, i'm trying to set the props and even the state of the Login component but it doesn't seem to take effect my guess is that MemoryRouter is blocking it somehow and setting props is actually setting the props of MemoryRouter rather than the Login component i could be wrong but thats what im thinking:
Any one figured a solution to this ?
The text was updated successfully, but these errors were encountered: