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 提示用户依赖需提前声明,否则会照成依赖丢失 #134

Closed HECHONG999 closed 7 months ago

HECHONG999 commented 8 months ago

问题描述: `js const [ state ]= share({ xxx: undefined } )

const result = derive(() => { if( !state.xxx ) return; // 前置返回依赖收集会丢失 return state.xxx + 1; });

`

优化 https://heluxjs.github.io/helux/guide/derive 文档,提示用户依赖需提前声明,否则会照成依赖丢失 😅

fantasticsoul commented 7 months ago

准确的描述应该是所有依赖都需要提前申明

bad

const result = derive(() => {
  if( state.x ) return 2;
  return state.y + 1;
});

ok

const result = derive(() => {
  const {x,y} = state;
  if(x ) return 2;
  y + 1;
});
fantasticsoul commented 7 months ago

文档新增 derive 注意事项 https://heluxjs.github.io/helux/guide/derive#%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9