ReactJs/Redux Invariant Violation: Could not find “store” in either the context or props of “Connect(LoginContainer)”

后端 未结 3 1498
深忆病人
深忆病人 2021-02-20 11:13

Not sure why I\'m getting this error, it happened when I added connect from redux to my Login component, so I could connect my store.

相关标签:
3条回答
  • 2021-02-20 11:36

    Redux recommends exporting the unconnected component for unit tests. See their docs.

    In login.js:

    // Named export for tests
    export class LoginContainer extends React.Component {
    }
    
    // Default export
    export default connect(null, mapDispatchToProps)(LoginContainer); 
    

    And in your test:

    // Import the named export, which has not gone through the connect function
    import { LoginContainer as Login } from './Login';
    

    You can then specify any props that would have come from the store directly on the component.

    0 讨论(0)
  • 2021-02-20 11:38

    You could use React's contextType or pass propType. You would need to declare it either as a prop or contextType.

    Provider.contextTypes = {
      Store: React.PropTypes.object.isRequired
    };
    
     Provider.propTypes= {
      Store: React.PropTypes.object.isRequired
    };
    
    0 讨论(0)
  • 2021-02-20 11:52

    You need to pass store as either a prop or context in your test. mount method accepts context as another parameter.

    and how do you get store here? You create store the same way you created in app.js

    0 讨论(0)
提交回复
热议问题