gpt4 book ai didi

javascript - 从容器中发送一个 Action

转载 作者:行者123 更新时间:2023-11-30 06:13:24 25 4
gpt4 key购买 nike

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

容器:

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;

添加国家/地区组件:

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;

Action 文件

import { ADD_COUNTRY } from './names'


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

我看不到 addCountry 作为 Prop ,我是不是错过了什么?

最佳答案

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

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

关于javascript - 从容器中发送一个 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57575390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com