Action does not trigger a reducer in React + Redux

后端 未结 2 1848
心在旅途
心在旅途 2021-01-18 02:32

I\'m working on a react-redux app and for some reason the action I call does not reach the reducer (in which I currently only have a log statement). I have attached the code

2条回答
  •  情歌与酒
    2021-01-18 02:57

    As Rick Jolly mentioned in the comments on your question, your onSearchPressed() function isn't actually dispatching that action, because addToSaved() simply returns an action object - it doesn't dispatch anything.

    If you want to dispatch actions from a component, you should use react-redux to connect your component(s) to redux. For example:

    const { connect } = require('react-redux')
    
    class MainView extends Component {
      onSearchPressed() {
        this.props.dispatchAddToSaved();
      }
      render() {...}
    }
    
    const mapDispatchToProps = (dispatch) => {
      return {
        dispatchAddToSaved: () => dispatch(addToSaved())
      }
    }
    
    module.exports = connect(null, mapDispatchToProps)(MainView)
    

    See the 'Usage With React' section of the Redux docs for more information.

提交回复
热议问题