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
Javascript 在Redux中进行调度切换而不将其保存为状态?_Javascript_Reactjs_Redux_React Redux_React Hooks - Fatal编程技术网

Javascript 在Redux中进行调度切换而不将其保存为状态?

Javascript 在Redux中进行调度切换而不将其保存为状态?,javascript,reactjs,redux,react-redux,react-hooks,Javascript,Reactjs,Redux,React Redux,React Hooks,我现在是怎么做的 我有一份物品清单。现在,当用户按下按钮X时,shouldShowItem被切换shouldShowItem最终位于redux中,并作为道具传递到Item中。它不是true就是false。当它更改时,将调用toggleDisplay并更改my hook中的状态: useEffect(() => { toggleDisplay(!display); //this is just a useState hook call }, [shouldShowItem]); //PS

我现在是怎么做的

我有一份物品清单。现在,当用户按下按钮X时,
shouldShowItem
被切换
shouldShowItem
最终位于redux中,并作为道具传递到
Item
中。它不是
true
就是
false
。当它更改时,将调用toggleDisplay并更改my hook中的状态:

useEffect(() => {
  toggleDisplay(!display); //this is just a useState hook call
}, [shouldShowItem]); //PS: I'm aware that I don't need this extra step here, but my actual code is a bit more complicated, so I just simplified it here.
我的问题是,我在redux中有一个
shouldShowItem
属性,而不是每个项目都有一个
shouldShowItem
。我不想将此属性移动到每个项目的redux状态

问题:

然而,我的构造存在的问题是,
shouldShowItem
正在保存,这意味着如果我在时间X为项目Y切换它,然后我的项目Z也由于一个不相关的事件而重新呈现,它将以更新的
shouldShowItem
状态重新呈现,尽管该状态更改是针对项目X的

本质上,我是在redux中保存
shouldShowItem
的状态,而
我只需要一个切换,可以发送一次,对当前项目有效,然后就不再读取/需要了。我想基本上分配一个切换,-我不关心redux中每个项目的状态,我只关心它被切换一次

建议

编辑:更多代码

项目:

const Item=({shouldShowItem,itemText,updateCurrentItem})=>
{
const[display,toggleDisplay]=useState(false);
useffect(()=>{
如果(当选){
切换显示(!显示);
}
},[shouldShowItem]);
返回(
{
切换显示(!显示);
更新当前项目(项目编号);
}}
>
{display&&{itemText}
);
}
通过项目列表映射:

  allItems.map((item, i) => {
        return (
          <Item
            key={i}
            shouldShowItem={this.props.shouldShowItem}
            itemText={item.text}
            updateCurrentItem={this.props.updateCurrentItem}
        );
      });
allItems.map((项目,i)=>{
返回(

与其使用布尔值
shouldShowItem
,不如将其转换为具有布尔值的ID对象:

const[showItem,setShowItem]=useState({id_1:false,id_2:false})
const-toggleDisplay=id=>{
setShowItem({…showItem,[id]:!showItem[id]})
更新当前项目(id)
}
allItems.map((项目,i)=>{
返回(
{
返回(
切换显示(项目编号)}
>
{shouldShowItem&&{itemText}}
)
}

您可以在redux存储中放置一个名为
activeItems
的属性-一个与您切换的项目相关的ID数组Hanks!我可以这样做,但我觉得我真的根本不需要保存此属性。我只想有一个切换,而不需要保存任何状态。/它也可能是一个反模式,我不确定,b但对于我的用例来说,它现在看起来非常整洁。你需要显示更多的代码:列表,这个按钮所在的位置,它应该做什么等等。@Lelouch更新了我的问题。按钮所在的位置并不重要,我相信,它只是调用一个动作创建者,切换
shouldShowItem
true/false。我想实现一个这样的动作创建者告诉我的项目组件:“嘿,现在更新!”但之后不需要保存状态。
  allItems.map((item, i) => {
        return (
          <Item
            key={i}
            shouldShowItem={this.props.shouldShowItem}
            itemText={item.text}
            updateCurrentItem={this.props.updateCurrentItem}
        );
      });