我是react-redux的新手,我想将一个操作从容器分派到组件。这是我的代码:

容器 :

import { connect } from 'react-redux';
import addCountryComponent from '../../views/Country/AddCountry'
import { addCountry } from '../../actions/countryActions'


const mapDispatchToProps = (dispatch) => {
  return {
    addCountry:(country) => dispatch(addCountry(country))
  }
}


const CountryContainer = connect(null, mapDispatchToProps)(addCountryComponent)
export default CountryContainer;


AddCountry组件:

import React, {Component} from 'react';

class AddCountry extends Component {
  constructor(props) {
    super(props);
  }
  componentDidMount() {
    console.log(this.props);
  }
  render() {
    return (
      <div className="animated fadeIn">
asdas
      </div>
    );
  }
}



export default AddCountry;


和动作文件

import { ADD_COUNTRY } from './names'


export function addCountry(payload) {
  console.log(payload, "actions")
  return {
    type: ADD_COUNTRY,
    payload,
  }
}


我看不到addCountry是道具,我缺少什么吗?

最佳答案

正如您在评论中提到的,您应该将容器链接到路线文件。

这意味着浏览器打开此/add/country时将调用您的容器

10-04 17:14