简体   繁体   English

CreateEntityAdapter 中的 SetAll 不使用 react redux 工具包设置状态

[英]SetAll from CreateEntityAdapter do not set the state with react redux toolkit

I am trying to use Redux Toolkit in my new website and I have a problem using the createEntityAdapter.我正在尝试在我的新网站中使用 Redux Toolkit,但在使用 createEntityAdapter 时遇到了问题。

I am fetching some data from my database but my state is never updated with the data fetched and I do not understand why.我正在从我的数据库中获取一些数据,但我的状态从未随获取的数据更新,我不明白为什么。

My slice and fetch functions:我的切片和获取功能:

export const getTransports = createAsyncThunk('marketplace/transports/getTransports', async email => {
    const response = await axios.get(`${API_URL}/transport/published/company/${email}/notActive`);
    console.log('response', response);
    const data = await response.data;
    console.log('DATA', data);
    return data;
});

const transportsAdapter = createEntityAdapter({});

export const { selectAll: selectTransports, selectById: selectTransportById } = transportsAdapter.getSelectors(
    state => state.marketplace.transports
);

const transportsSlice = createSlice({
    name: 'marketplace/transports',
    initialState: transportsAdapter.getInitialState({
        searchText: ''
    }),
    reducers: {
        setTransportsSearchText: {
            reducer: (state, action) => {
                state.searchText = action.payload;
            },
            prepare: event => ({ payload: event.target.value || '' })
        },
        extraReducers: {
            [getTransports.fulfilled]: transportsAdapter.setAll
        }
    }
});

export const { setTransportsSearchText } = transportsSlice.actions;

export default transportsSlice.reducer;

The data fetch is working well and the state between the request and de fullfilled looks like working as it should be, but as you can see in the console, the transports state is never updated.数据获取运行良好,请求和 de fullfill 之间的状态看起来像它应该的那样工作,但正如您在控制台中看到的那样,传输状态永远不会更新。

Redux Logger Redux 记录器

I do not understand why is not working the setAll function from the transportsAdapter.我不明白为什么不能使用 transportsAdapter 的 setAll 函数。 The entities that are being retrieved have and id and the entity information, so it should work correctly but it does not.正在检索的实体具有 id 和实体信息,因此它应该可以正常工作,但事实并非如此。

I hope you can help me.我希望你能帮助我。

Thank you very much.非常感谢。

I was facing the same issue following along the Redux Essentials Tutorial .Redux Essentials Tutorial之后,我遇到了同样的问题。

I fixed it by specifying the parameters to the function setAll() and using the builder callback approach.我通过为函数setAll()指定参数并使用构建器回调方法来修复它。


const transportsSlice = createSlice({
    name: 'marketplace/transports',
    initialState: transportsAdapter.getInitialState({
        searchText: ''
    }),
    reducers: {
        setTransportsSearchText: {
            reducer: (state, action) => {
                state.searchText = action.payload;
            },
            prepare: event => ({ payload: event.target.value || '' })
        },
        extraReducers: (builder) => {
            builder.addCase(getTransports.fulfilled, (state, action) => {
              transportsAdapter.setAll(state, action.payload);
        });
    },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM