heluxjs / helux

A reactive atomic state engine for React like.
https://heluxjs.github.io/helux/
MIT License
1.33k stars 70 forks source link

derive 派生函数如何捕获状态中可选链的依赖问题(How does the derived function capture the dependency problem of optional chaining in the state?) #136

Closed HECHONG999 closed 8 months ago

HECHONG999 commented 8 months ago

问题描述:

在派生函数内使用到state里可选链state,如 state.currentTaskView?.taskFieldVisibleConfig,依赖不会收集可选链后的状态 taskFieldVisibleConfig 的状态不会被收集

In the derived function, use the optional chain state in state, such as state.currentTaskView?.taskFieldVisibleConfig. The dependency will not collect the status of taskFieldVisibleConfig after the optional chain.

示例

export const viewDisplayTaskFields = (): TTaskFieldVisibleConfig[] => {
    const taskFieldVisibleConfig = state.currentTaskView?.taskFieldVisibleConfig || [];
    const taskTypeArray = state.taskTypeList || [];
    debugger;
     return X
}

临时解决

    setProjectInfo((draft) => {
                const taskTypeIndex = draft.currentTaskView?.taskFieldVisibleConfig?.findIndex(
                    (item) => item.taskType.id === projectInfo.currentTaskType?.id,
                );
                if (taskTypeIndex !== undefined && fieldIndex !== undefined && draft.currentTaskView) {
                    draft.currentTaskView.taskFieldVisibleConfig[taskTypeIndex].taskType.boardProperties[fieldIndex].visible =
                        true;
                      // resolve 手动更新一下可选链前的状态
                    draft.currentTaskView = { ...draft.currentTaskView };

                }
            });
fantasticsoul commented 8 months ago

fixed at >= 4.2.0