Answer the question
In order to leave comments, you need to log in
Next.js + Redux does not update state. Where is the mistake?
Essence such:
1. On page in getInitialProps I call the asynchronous function receiving statistics of the user.
class Statistics extends React.Component<StatisticsProps> {
static async getInitialProps({ req, res }: NextPageContext) {
const token = getCookie('authtoken', req);
if (!token) {
redirect('/auth', res);
return false;
}
store.dispatch(requestUserStatistics());
StatisticsService.getUserStatistics();
}
// Остальной код страницы
export default class StatisticsService {
static async getUserStatistics(params?: Readonly<{ [key: string]: any }>) {
const res = await post<StatisticsResponse>(`/api/economics/lk/stat/`, params);
if(!res.data.error) {
// console.log("disp disp disp", res.data)
store.dispatch(receiveUserStatistics({result: res.data.result, result_total: res.data.result_total, chartFields: ["users", "visits"]}));
}
return res.data;
}
}
case RECEIVE_USER_STATISTICS:
console.log("received", action.statistics)
return {
...state,
statistics: action.statistics,
isLoadingStatistics: false
};
componentDidMount() {
store.dispatch(requestUserStatistics());
StatisticsService.getUserStatistics();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question