状态不变时 Redux 不断重新渲染
Redux keeps re-rendering when state doesn't change
我正在制作我的第一个 React-Redux 项目。
我学会了仅在组件状态发生变化时才触发的 React 重新渲染。
可是我现在很迷茫
因为状态没有改变,但组件每 1 到 3 秒重新渲染一次。
我试过 shallowEqual,但没用。
为什么会这样?
// container
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import Home from 'presentations/Home';
import * as homeActions from 'modules/home';
const HomeContainer = () => {
const dispatch = useDispatch();
const list = useSelector(state => state.home.list);
dispatch(homeActions.getList());
console.log(list);
return (
<Home
/>
);
}
export default HomeContainer;
// Redux module
import axios from 'axios';
import { call, put, takeEvery } from 'redux-saga/effects';
import { createAction, handleActions } from 'redux-actions';
function getListAPI() {
return axios.get('http://localhost:8000/');
}
const GET_LIST = 'home/GET_LIST';
const GET_LIST_SUCCESS = 'home/GET_LIST_SUCCESS';
const GET_LIST_FAILURE = 'home/GET_LIST_FAILURE';
export const getList = createAction(GET_LIST);
function* getListSaga() {
try {
const response = yield call(getListAPI);
yield put({ type: GET_LIST_SUCCESS, payload: response });
} catch (e) {
yield put({ type: GET_LIST_FAILURE, payload: e });
}
}
const initialState = {
list: []
};
export function* homeSaga() {
yield takeEvery('home/GET_LIST', getListSaga);
}
export default handleActions(
{
[GET_LIST_SUCCESS]: (state, action) => {
const temp = action.payload.data;
return {
list: temp
};
}
}, initialState
);
您正在功能组件体内调用 dispatch = useDispatch();
,它会更新状态 list
,这会导致重新渲染,然后执行您的 dispatch = useDispatch();
,依此类推。
在 useEffect 中调度您的操作以解决问题
const HomeContainer = () => {
const dispatch = useDispatch();
const list = useSelector((state) => state.home.list);
useEffect(() => { //<---like this
dispatch(homeActions.getList());
}, []);
console.log(list);
return <Home />;
};
我正在制作我的第一个 React-Redux 项目。
我学会了仅在组件状态发生变化时才触发的 React 重新渲染。
可是我现在很迷茫
因为状态没有改变,但组件每 1 到 3 秒重新渲染一次。
我试过 shallowEqual,但没用。
为什么会这样?
// container
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import Home from 'presentations/Home';
import * as homeActions from 'modules/home';
const HomeContainer = () => {
const dispatch = useDispatch();
const list = useSelector(state => state.home.list);
dispatch(homeActions.getList());
console.log(list);
return (
<Home
/>
);
}
export default HomeContainer;
// Redux module
import axios from 'axios';
import { call, put, takeEvery } from 'redux-saga/effects';
import { createAction, handleActions } from 'redux-actions';
function getListAPI() {
return axios.get('http://localhost:8000/');
}
const GET_LIST = 'home/GET_LIST';
const GET_LIST_SUCCESS = 'home/GET_LIST_SUCCESS';
const GET_LIST_FAILURE = 'home/GET_LIST_FAILURE';
export const getList = createAction(GET_LIST);
function* getListSaga() {
try {
const response = yield call(getListAPI);
yield put({ type: GET_LIST_SUCCESS, payload: response });
} catch (e) {
yield put({ type: GET_LIST_FAILURE, payload: e });
}
}
const initialState = {
list: []
};
export function* homeSaga() {
yield takeEvery('home/GET_LIST', getListSaga);
}
export default handleActions(
{
[GET_LIST_SUCCESS]: (state, action) => {
const temp = action.payload.data;
return {
list: temp
};
}
}, initialState
);
您正在功能组件体内调用 dispatch = useDispatch();
,它会更新状态 list
,这会导致重新渲染,然后执行您的 dispatch = useDispatch();
,依此类推。
在 useEffect 中调度您的操作以解决问题
const HomeContainer = () => {
const dispatch = useDispatch();
const list = useSelector((state) => state.home.list);
useEffect(() => { //<---like this
dispatch(homeActions.getList());
}, []);
console.log(list);
return <Home />;
};