Redux:未调用异步动作创建者
Redux: Asynchronous action creator not being called
我正在使用 redux-thunk
创建异步操作,以便我可以使用 'fetch' 调用 API 方法。我已将我的 redux
存储附加到我的根 App
组件,如下所示:
import React, { Component } from 'react';
import {Provider} from 'react-redux';
import Grid from '../src/Components/Grid/Grid';
import rootReducer from './Reducer';
import './App.css';
import { createStore,applyMiddleware } from 'redux';
import ReduxThunk from 'redux-thunk';
import logger from 'redux-logger';
class App extends Component {
render() {
return (
<Provider store={createStore(rootReducer,{},applyMiddleware(ReduxThunk))}>
<div className="App">
<Grid></Grid>
</div>
</Provider>
);
}
}
export default App;
我已经定义了我的异步动作创建者,如下所示:
import config from 'react-global-configuration';
import fetch from 'cross-fetch';
export default function textChangeActionCreator(data){
console.log('in action createor'+data)
return async function(dispatch){
console.log('fetching....')
return await fetch(data).
then((data)=>{
console.log(data);
dispatch({type:'text_change',payload:data})
}).
catch((err)=>{console.log(err)})
}
}
我的问题是,当我从它被正确调用的任何组件调用上述动作创建器时,第二个 console.log('fetching....')
没有出现并且 fetch()
调用似乎没有发生。
我做错了什么?
更新
这是调用动作创建者的组件代码:
class Grid extends React.PureComponent{
constructor(props)
{
super(props);
this.state={dynamicButtonText:'',seachText:''}
this.updateDynamicText=this.updateDynamicText.bind(this);
this.onTextChanged=this.onTextChanged.bind(this);
this.updateSearchText=this.updateSearchText.bind(this);
}
onTextChanged()
{
textChangeActionCreator(searchURL);
}
render()
{....}
}
const mapStateToProps=(state)=>{return state;}
export default connect(mapStateToProps,{textChangeActionCreator})(Grid);
尝试这样的事情:
const textChangeActionCreator = (data) => {
console.log(`in action createor ${data}`);
return (dispatch) => {
console.log('fetching...');
return fetch(data)
.then((data)=>{
console.log(data);
dispatch({ type:'text_change', payload:data });
})
.catch((err)=>{ console.log(err); });
};
};
您不需要 await 或 async,因为您正在返回一个承诺,您应该在调用此函数的地方等待。
此外,您在这里使用柯里化,所以您必须调用如下内容:
const fetchedData = await textChangeActionCreator(data)(dispatch);
这通常是由不正确的操作调用引起的。确保你们都在调用 textChangeActionCreator()
动作创建者,并将结果传递给 propsToDispatch
对象中的 dispatch()
,然后传递给 connect()
:
export default connect(mapStateToProps, dispatch => {
/* Call textChangeActionCreator() and pass the result to dispatch() */
return {
textChange : (data) => dispatch(textChangeActionCreator(data))
}
})(YourComponent);
更新
刚刚检查了您的代码,您似乎是直接从 onTextChanged()
调用动作创建器。您需要通过组件的 props
调用它,如下所示:
onTextChanged()
{
this.props.textChangeActionCreator(searchURL);
}
我正在使用 redux-thunk
创建异步操作,以便我可以使用 'fetch' 调用 API 方法。我已将我的 redux
存储附加到我的根 App
组件,如下所示:
import React, { Component } from 'react';
import {Provider} from 'react-redux';
import Grid from '../src/Components/Grid/Grid';
import rootReducer from './Reducer';
import './App.css';
import { createStore,applyMiddleware } from 'redux';
import ReduxThunk from 'redux-thunk';
import logger from 'redux-logger';
class App extends Component {
render() {
return (
<Provider store={createStore(rootReducer,{},applyMiddleware(ReduxThunk))}>
<div className="App">
<Grid></Grid>
</div>
</Provider>
);
}
}
export default App;
我已经定义了我的异步动作创建者,如下所示:
import config from 'react-global-configuration';
import fetch from 'cross-fetch';
export default function textChangeActionCreator(data){
console.log('in action createor'+data)
return async function(dispatch){
console.log('fetching....')
return await fetch(data).
then((data)=>{
console.log(data);
dispatch({type:'text_change',payload:data})
}).
catch((err)=>{console.log(err)})
}
}
我的问题是,当我从它被正确调用的任何组件调用上述动作创建器时,第二个 console.log('fetching....')
没有出现并且 fetch()
调用似乎没有发生。
我做错了什么?
更新
这是调用动作创建者的组件代码:
class Grid extends React.PureComponent{
constructor(props)
{
super(props);
this.state={dynamicButtonText:'',seachText:''}
this.updateDynamicText=this.updateDynamicText.bind(this);
this.onTextChanged=this.onTextChanged.bind(this);
this.updateSearchText=this.updateSearchText.bind(this);
}
onTextChanged()
{
textChangeActionCreator(searchURL);
}
render()
{....}
}
const mapStateToProps=(state)=>{return state;}
export default connect(mapStateToProps,{textChangeActionCreator})(Grid);
尝试这样的事情:
const textChangeActionCreator = (data) => {
console.log(`in action createor ${data}`);
return (dispatch) => {
console.log('fetching...');
return fetch(data)
.then((data)=>{
console.log(data);
dispatch({ type:'text_change', payload:data });
})
.catch((err)=>{ console.log(err); });
};
};
您不需要 await 或 async,因为您正在返回一个承诺,您应该在调用此函数的地方等待。
此外,您在这里使用柯里化,所以您必须调用如下内容: const fetchedData = await textChangeActionCreator(data)(dispatch);
这通常是由不正确的操作调用引起的。确保你们都在调用 textChangeActionCreator()
动作创建者,并将结果传递给 propsToDispatch
对象中的 dispatch()
,然后传递给 connect()
:
export default connect(mapStateToProps, dispatch => {
/* Call textChangeActionCreator() and pass the result to dispatch() */
return {
textChange : (data) => dispatch(textChangeActionCreator(data))
}
})(YourComponent);
更新
刚刚检查了您的代码,您似乎是直接从 onTextChanged()
调用动作创建器。您需要通过组件的 props
调用它,如下所示:
onTextChanged()
{
this.props.textChangeActionCreator(searchURL);
}