在我的组件中,我从 API 加载初始数据,如下所示:
const dispatch = useDispatch();
const groups = useSelector(selectGroups);
const [localGroupState, setLocalGroupsState] = useState(groups);
useEffect(() => {
dispatch(loadAsync());
}, []);
export const selectGroups = (state: RootState) => state.userGroups.groups;
export const loadAsync = (): AppThunk => dispatch => {
dispatch(loading());
axios.get('/data', { headers: { 'Authorization': `Bearer ${getToken()}` } })
.then((axiosResponse: AxiosResponse<MainState>) => {
dispatch(loaded(axiosResponse.data));
console.log('all good')
})
.catch(() => {
console.error('no good')
});
};
但是localGroupState
是空的却不groups
是。我觉得我在这里错过了一个简单的技巧。非常感谢您的帮助。