简体   繁体   English

Redux 数据更新不调度

[英]Redux data updating without dispatching

I have redux data and it's updating even if dispatch is firing, below is my redux state我有 redux 数据,即使调度正在触发,它也在更新,下面是我的 redux state

const initialState = {
    actions: [
        {
            id: 'c943df13-549b-4bf9-838c-4c5da1e9649a',
            userId: 'badd523d-62ea-4763-a955-92a612861495',
            jobId: '5aa44d3d-b925-462b-befb-b7c673c2dfae',
            description: 'Test Description 1',
            timeTaken: 2,
            nonBillable: false,
            status: 0,
            dateCreated: new Date(),
        },
        {
            id: '4dcd6ca4-42dc-4868-bc4b-a08c849f5846',
            userId: 'badd523d-62ea-4763-a955-92a612861495',
            jobId: '5aa44d3d-b925-462b-befb-b7c673c2dfae',
            description: 'Test Description 2',
            timeTaken: 5,
            nonBillable: false,
            status: 0,
            dateCreated: new Date(),
        },
    ],
};

const reducers = (state: any = initialState, action: any) => {
    switch (action.type) {
        default:
            return state;
    }
};

export default reducers;

moving forward, I am fetching this data via useEffect and useSelector and has a filter of a jobId继续前进,我正在通过 useEffect 和 useSelector 获取这些数据,并且有一个 jobId 过滤器

const reducers = useSelector((state: RootState) => state.reducers);
const [items, setItems] = useState<Array<ActionProps>>([]);

...

useEffect(() => {
        
        const actItems = reducers.actions.filter(
            (item: ActionProps) => item.jobId === jobId
        );

        setItems(actItems);
    }, [jobId, reducers.actions]);

now, since the redux data is now inside of the state I am just updating the nonBilable object inside of it because I need to update the front end as it is clickable via form and here's my function now, since the redux data is now inside of the state I am just updating the nonBilable object inside of it because I need to update the front end as it is clickable via form and here's my function

const onChangeToggle = ({ id, value }: { id: string | number; value: boolean }) => {
        const currentData = [...items];
        const findIndex = currentData.findIndex((item: ActionProps) => item.id === id);
        currentData[findIndex].nonBillable = value;

        setItems(currentData);
    };

As you can see above function, I am not dispatching the update since I have another function to dispatch it.正如您在上面的 function 中看到的,我没有发送更新,因为我还有另一个 function 来发送它。 Now, when I try to navigate to another page and go back to the page the state is being saved even if I have a useEffect inside the component and not dispatching.现在,当我尝试导航到另一个页面并将 go 返回到该页面时,即使我在组件中有 useEffect 并且没有调度,state 也会被保存。

I have found this Redux state is being updated without dispatching any action question but I think we have a different situation我发现这个Redux state 正在更新,但没有发送任何操作问题,但我认为我们有不同的情况

Any help is appreciated.任何帮助表示赞赏。

Looks like a reference issue, can you try deep cloning the reducer and set it to state like this.看起来像一个参考问题,您可以尝试深度克隆减速器并将其设置为 state 像这样。

useEffect(() => {

      const clonedReducers = JSON.parse(JSON.stringify(reducers));  
      const actItems = clonedReducers.actions.filter(
            (item: ActionProps) => item.jobId === jobId
        );

        setItems(actItems);
    }, [jobId, reducers.actions]);

Because when you do the below code因为当您执行以下代码时

currentData[findIndex].nonBillable = value;

The reference of currentData in your state looks like pointing to the same reference in the reducer. state 中currentData的引用看起来像是指向减速器中的相同引用。 So mutating it directly mutates the value in the reducer as well.所以改变它也会直接改变reducer中的值。

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

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