I am trying to test my React component and getting the following error.
Invariant Violation: Could not find "store" in either the context or props of "Connect()". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect()".
The error comes while rendering the Component in the test.
beforeEach(() => {
Component = TestUtils.renderIntoDocument(<SideMenu />);
});
It works fine when rendering the Component on the page. However in test, I am not able to pass the store explicitly into the Component.
Can someone point in the right direction?
From Parent to Child Using Props 1class Parent extends React. Component {state = { data : "Hello World" } 2render() { 3 4 return ( 5 <div> 6 <Child1/> //no data to send 7 <Child2 dataFromParent = {this. state. data} /> 8 </div> 9 ); } 10} 11//Sending data as a state is not obligatory.
You can use the <></> Fragments to pass the HTML in the props.
Sending state/props to another component using the onClick event: So first we store the state/props into the parent component i.e in which component where we trigger the onClick event. Then to pass the state into another component, we simply pass it as a prop.
To answer the question (I ran into this and the accepted answer is not what I needed), create a new method like:
function connectWithStore(store, WrappedComponent, ...args) {
let ConnectedWrappedComponent = connect(...args)(WrappedComponent)
return function (props) {
return <ConnectedWrappedComponent {...props} store={store} />
}
}
Then, for connect, use:
const ConnectedApp = connectWithStore(store, App, mapStateToProps, mapDispatchToProps,)
export default ConnectedApp;
See here: https://github.com/reactjs/react-redux/issues/390#issuecomment-221389608
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With