使用React、Redux、Redux-thunk,我想通过服务器请求(API调用)获得一个初始状态,但我似乎无法让它工作。
到目前为止,我得到的是:
var Redux = require('redux');
var carReducer = require('./reducers/cars');
var thunk = require('redux-thunk').default;
var initialState = require('./reducers/initialState');
var rootReducer
我正在构建一个应用程序,在此应用程序中,我需要在挂载主要组件之前从服务器获取初始redux状态。在我的操作中,我使用redux-thunk来获取必要的数据:
export const getInitialStateSuccess = (state: any): IfetchThemeSuccess => {
console.log('state', state);
return {
type: FETCH_INIT_STATE,
state
};
};
// here I fetch the data from
我是React/Redux的新手。任何帮助都会很好。我有一个组件,它对ComponentDidMount进行API调用以获取数据,然后更新我的Redux存储。该组件还使用connect获取Redux状态,并将其作为道具传递给哑组件。
componentDidMount() {
this.props.dispatch(fetchSite()) //this triggers the api call and updates the redux store.
}
return (
<div>
<Child
myprop={this.props.
我可能不完全理解校长,哦,useState钩子,有一个问题,我遇到了几次。
我试图使用没有依赖项的useEffect在组件中设置初始状态,稍后将其与从redux获得的其他变量进行比较。
// get the customer saved in redux
const Customer = useGetCustomer();
const [prevCustomerNumber, setPrevCustomerNumber] = useState(null)
useEffect(() => {
// if there is already customer saved in redux,