Reactjs 如何修复错误:应为赋值或函数调用,而不是看到表达式没有未使用的表达式
我仍然不知道如何从我的操作中获取数据 行动如下:Reactjs 如何修复错误:应为赋值或函数调用,而不是看到表达式没有未使用的表达式,reactjs,redux,Reactjs,Redux,我仍然不知道如何从我的操作中获取数据 行动如下: import myConstant from './constatnts' ... export const fetchData = { isLoading, isSuccess, ... }; const isLoading = () => dispatch => { dispatch({ type: myConstant.FETCH_LOADING, payload: LoadingApi()
import myConstant from './constatnts'
...
export const fetchData = {
isLoading,
isSuccess,
...
};
const isLoading = () => dispatch => {
dispatch({
type: myConstant.FETCH_LOADING,
payload: LoadingApi()
});
}
并尝试使用ES5:
function isSuccess() {
return function(dispatch){
dispatch({
type: myConstant.FETCH_DATA
payload: fetchDataApi()
});
};
}
我在组件中将数据获取到Redux时出错:
import {fetchData} from './myData'
componentWillMount() {
fetchData.isSuccess; // or fetchData.isLoading
}
我有以下错误:
应为赋值或函数调用,而不是看到表达式
没有未使用的表达式
如何解决此问题?您需要使用dispatch keywoard调用您的操作。如果要使用react组件中的dispatch关键字,则需要使用
react redux
包中的connect()
函数包装组件
接下来,您将需要定义至少mapstatetops
函数来获取数据(编辑:您不需要包含mapstatetops:)仅当您希望将一些数据返回到组件时才包含它),这些数据是您通过redux存储作为道具保存到存储中的
因此,您的代码应该如下所示:
import {fetchData} from './myData'
import {connect} from "react-redux";
class YourComp extends Component {
...
componentWillMount() {
this.props.dispatch(fetchData.isSuccess());
}
...
}
// this will push data from the store to your props
const mapStateToProps = state => ({
yourData: state.reducer.data
});
// besides other things, this line will push dispatch function callback into your component's props
export default connect(mapStateToProps)(YourComp);
谢谢,这有助于我理解!