Javascript 未调用Thunk操作体
我的thunk动作似乎没有贯穿其核心逻辑。我从Javascript 未调用Thunk操作体,javascript,reactjs,react-redux,redux-thunk,Javascript,Reactjs,React Redux,Redux Thunk,我的thunk动作似乎没有贯穿其核心逻辑。我从componentDidMount中调用了thunk操作,但它不会反过来导致运行:const response=wait findOne(id) 另外,我认为如果使用redux thunk,我不需要明确地将分派作为道具传递给mapDispatchToProps,我认为我的thunk设置方式是分派已经可以用于thunk?我用过其他类似的动作,效果很好,为什么不用这个呢 Thunk动作 export function fetchCompany(id) {
componentDidMount
中调用了thunk操作,但它不会反过来导致运行:const response=wait findOne(id)
另外,我认为如果使用redux thunk,我不需要明确地将分派作为道具传递给mapDispatchToProps,我认为我的thunk设置方式是分派已经可以用于thunk?我用过其他类似的动作,效果很好,为什么不用这个呢
Thunk动作
export function fetchCompany(id) {
return async (dispatch) => {
try {
const response = await findOne(id)
if(response && response.body) {
const company = response.body
dispatch(companyReceived(company))
}
} catch(err) {
console.log("failed request in authenticate thunk action")
console.log(`error details: ${err.status} /n ${err}`)
}
}
}
export function fetchCompanies() {
return async (dispatch, getState) => {
const response = await find()
if(response && response.body) {
const companies = response.body
dispatch(companiesReceived(companies))
}
}
}
容器
在
InterviewContainer
的componentDidMount
中,如果将函数对象作为connect
的mapDispatchToProps
参数传递,则会意外调用导入的fetchCompany
,而不是this.props.fetchCompany,每个函数都包装在dispatch
中,因此您的mapDispatchToProps
不是问题所在。更多信息,请参阅。是的,我是个白痴,我也喜欢这样。啊。谢谢
class HomePageContainer extends Component {
constructor(){
super()
}
async componentDidMount() {
await this.props.fetchFeaturedCompanies()
await this.props.fetchCompanies()
await this.props.fetchCountries()
}
render(){
return (<HomePage className='ft-homepage'
featuredCompanies={this.props.featuredCompanies}
countries={this.props.countries}
companies={this.props.companies}
/>)
}
}
const mapStateToProps = state => ({
countries: state.country.countries,
companies: state.company.companies,
featuredCompanies: state.company.featuredCompanies
})
const mapDispatchToProps = {
fetchCountries: fetchCountries,
fetchCompanies: fetchCompanies,
fetchFeaturedCompanies: fetchFeaturedCompanies
}
export default connect(mapStateToProps, mapDispatchToProps)(HomePageContainer)
export function fetchCompanies() {
return async (dispatch, getState) => {
const response = await find()
if(response && response.body) {
const companies = response.body
dispatch(companiesReceived(companies))
}
}
}