如何更新React Redux状态

问题描述 投票:0回答:1

我在代码顶部获得了使用useSelectore以状态登录的用户的预定义数据,但是当我想更改用户个人资料的值时

onSubmit=
    {async values => {
                        await updateUser(values).then((data) => {
                        store.dispatch(setCurrentUser(data));
                        store.subscribe();
                        console.log("store", store.getState().auth.user);//it shows the state has been updated

      });
    }}

它在视图中进行了更改并在数据库中进行了更新,但是当我刷新页面时,它显示了商店中的旧值。我的减速器

const auth = createSlice({
name: 'auth',
initialState,
reducers: {
    setCurrentUser : (state, action) => {
        state.isAuthenticated = true;
        state.user = action.payload;

    }....})

更新状态的documentation使我更加困惑

 function select(state) {
  return state.some.deep.property
 }

let currentValue
function handleChange() {
 let previousValue = currentValue
 currentValue = select(store.getState())

 if (previousValue !== currentValue) {
   console.log(
     'Some deep nested property changed from',
     previousValue,
     'to',
     currentValue
    )
 }
}

 const unsubscribe = store.subscribe(handleChange)
 unsubscribe()
reactjs react-redux store subscribe
1个回答
0
投票

Redux是内存存储。然后刷新页面,它将消失。

因此,您必须使用localStorage或indexedDB来存储数据。

为了更好地理解[C0,请看一下这样的库

您需要从localStorage获取初始状态,并在每次更改时将redux状态保持到localStorage。

© www.soinside.com 2019 - 2024. All rights reserved.