Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用“重新选择”访问当前的重新选择状态?_Javascript_Reactjs_Ecmascript 6_Redux - Fatal编程技术网

Javascript 如何使用“重新选择”访问当前的重新选择状态?

Javascript 如何使用“重新选择”访问当前的重新选择状态?,javascript,reactjs,ecmascript-6,redux,Javascript,Reactjs,Ecmascript 6,Redux,我想基于同一选择器的最后一个值返回一个值。e、 g const myReselect = createSelector( [otherSelector], (otherValue) => { // how to access current state? return state.myReselect + 1 } ) const globalState = state => ({ myReselect: myReselect(state) }) 您

我想基于同一选择器的最后一个值返回一个值。e、 g

const myReselect = createSelector(
  [otherSelector],
  (otherValue) => {
    // how to access current state?
    return state.myReselect + 1
  }
)

const globalState = state => ({
  myReselect: myReselect(state)
})

您可以组合多个选择器来创建更复杂的选择

例如,您有一个
selectorA
,它返回一些数据,然后您想将
selectorA
中的数据和
state.dataB
中的一些其他数据组合在一起

创建一个额外的
选择ORB
,并制作一个最终的组合版本:

// this is reusable selector
import selectorA from './selectorA';

// this is our local selector 
const selectorB = state => state.dataB;

const finalSelector = createSelector(
   selectorA,
   selectorB,
   (a, b) => {
     return a + b;
   }
);

// pubish our resulting selector
export default finalSelector;

finalSelector
只有在某些依赖选择器发生更改时才会更新

这不是一种坏做法吗?因为createSelector背后的要点是,只有在某些输入发生更改时才会重新运行,从而防止昂贵的逻辑多次运行。这将使myReselect可以在状态的任何部分发生变化时运行,即使是我不关心的部分。当然,你是对的。近一年来,我也学到了一些东西。请参阅我的最新答案。