Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass store explicitly into as a prop to "Connect()"

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?

like image 932
Salman Avatar asked Sep 22 '15 03:09

Salman


People also ask

How do you pass data into props?

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.

Can you pass HTML as a prop?

You can use the <></> Fragments to pass the HTML in the props.

How do you pass a state as a prop to another component?

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.


1 Answers

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

like image 185
mikeb Avatar answered Sep 19 '22 19:09

mikeb