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
Reactjs React js,将redux状态存储到本地状态的最佳方法是什么?_Reactjs_Redux - Fatal编程技术网

Reactjs React js,将redux状态存储到本地状态的最佳方法是什么?

Reactjs React js,将redux状态存储到本地状态的最佳方法是什么?,reactjs,redux,Reactjs,Redux,我有表的缩减器,其中一个状态是mainTables。我正在使用react dnd定位这些表 const initialState = { channel: null, errors: null, mainTables: [], takeaways: [], showMainTables: false, showTakeaways: false, }; 现在,我的表\u容器如下所示 constructor() { super(); this.state = { tables

我有
表的缩减器
,其中一个状态是
mainTables
。我正在使用
react dnd
定位这些表

const initialState = {
 channel: null,
 errors: null,
 mainTables: [],
 takeaways: [],
 showMainTables: false,
 showTakeaways: false,
};
现在,我的
表\u容器
如下所示

constructor() {
 super();
 this.state = {
   tables: []
 }
 this.fetchTables = this.fetchTables.bind(this);
 this.moveTable = this.moveTable.bind(this);
 this.saveTables = this.saveTables.bind(this);
}

componentDidMount(){
 this.fetchTables()
}

fetchTables () {
  httpGet('/api/v1/tables')
  .then((response) => {
    this.setState({
      tables: response.main_tables
    });
  });
};
table\u container
是一个表格容器,用户可以在其中移动
table
,通过
拖放来定位它。尽管我在reducer中有
mainTables
,其中存储了
mainTable
的所有对象,但我使用
fetchTables
main_表存储到
表的本地状态,因为我希望用户移动表进行测试,然后如果他们喜欢,可以单击
保存
按钮来存储它。如果他们不喜欢,他们可以离开页面。所以,当用户移动表时,它不必一直更改redux状态。但这种方法存在一个问题。当我在
componentDidMount
中使用
fetchTable()
时,当用户创建表时,它不会更新
this.state.tables
。所以要查看刚刚创建的表,用户必须刷新页面

所以,我想问的是 1) 如何将redux状态存储到本地状态(
this.state
)并在redux状态更新时更新?可能吗? 2) 创建新表时,我可以调用
fetchTables
?怎么做

提前谢谢

----编辑添加渲染方法

render() {
if (this.props.tables.showMainTables ==true){
const { hideSourceOnDrag, connectDropTarget, number } = this.props;
const { tables } = this.state;
return connectDropTarget(
  <div style={styles}>
    {Object.keys(tables).map((key) => {
      const { x, y, number } = tables[key];
      return (
        <Table
          key={key}
          id={key}
          x={x}
          y={y}
          number={number}>

        </Table>
        );
    })}
    <button onClick={this.saveTables}>Save</button>
  </div>,
);

}

else return false;
}
render(){
if(this.props.tables.showMainTables==true){
const{hideSourceOnDrag,connectDropTarget,number}=this.props;
const{tables}=this.state;
返回connectDropTarget(
{Object.keys(tables).map((key)=>{
常数{x,y,number}=表[key];
返回(
);
})}
拯救
,
);
}
否则返回false;
}

通常,您需要创建一个存储,例如TableStore,它将包含初始状态和任何外部操作(如网络调用)发生后的任何后续状态

您的组件将订阅它。每次状态更改时,存储都会让您的组件知道。然后,您可以简单地执行类似于
this.setState(tablestore.getTablesState())

不鼓励从组件发送获取请求。
此外,如果您正在寻找一个完整的架构来为您的应用程序奠定基础,我不认为这是最好的地方,因为这对于教程来说很容易变得足够长。

通常,您需要创建一个存储,例如tablestore,它将包含初始状态和任何外部操作(如网络调用)发生后的任何后续状态

您的组件将订阅它。每次状态更改时,存储都会让您的组件知道。然后,您可以简单地执行类似于
this.setState(tablestore.getTablesState())

不鼓励从组件发送获取请求。 另外,如果你正在寻找一个完整的架构来为你的应用程序打下基础,我不认为这是最好的地方,因为这对于一个教程来说已经足够长了。

我使用
构造函数(props)
组件将接收props
来将全局状态存储到本地状态

因此在视图中,我创建了
const mainTable=this.props.tables.mainTables
并将
mainTable
传递给
table\u容器

constructor(props) {
 super(props);
  this.state = {
   tables: props.mainTables
 }
}
并制作了如上所述的构造器。为了获得更新的数据并将其更新到本地状态,我使用了
componentWillReceiveProps

componentWillReceiveProps(nextProps){
if(this.state.tables.length != nextProps.mainTables.length){
  this.setState({tables: nextProps.mainTables})
}}
我使用
constructor(props)
componentWillReceiveProps
将全局状态存储到本地状态

因此在视图中,我创建了
const mainTable=this.props.tables.mainTables
并将
mainTable
传递给
table\u容器

constructor(props) {
 super(props);
  this.state = {
   tables: props.mainTables
 }
}
并制作了如上所述的构造器。为了获得更新的数据并将其更新到本地状态,我使用了
componentWillReceiveProps

componentWillReceiveProps(nextProps){
if(this.state.tables.length != nextProps.mainTables.length){
  this.setState({tables: nextProps.mainTables})
}}

谢谢你的回复。但我不完全理解你的答案。。。我想做的是将
tables
的初始状态的
mainTables
存储到本地组件的状态
this.state.tables
。。我将
mainTables
传递给本地组件,并尝试在
componentDidMount
render
中设置状态。。所以我在寻找如何将
mainTables
存储到本地组件的状态。我能从中得到的是,表是一个散列。因此,在构造函数中将其设置为
{}
而不是
[]
,并且
this.props.tables.showMainTables
是否设置为false?您是否尝试过调试以确定是否存在这种情况?您使用的是
this.props.tables.showMainTables
而不是
this.state.tables.showMainTables
您希望它如何工作?您是否通过props传递tables对象?正如我的问题中所述,我有
tables
reducer,它存储与
tables
相关的所有状态。其中一个状态是
showMainTables
。我有许多表格类型,因此我使用
showMainTables
showtakaways
来启用查看或不查看。这些都不是问题。。我的问题是如何在
tables
reducer中获取
mainTables
,并将
mainTables
存储为组件的本地状态
mainTables
是一个数组,其中包含许多table对象。感谢您的回复。但我不完全理解你的答案。。。我想做的是将
tables
的初始状态的
mainTables
存储到本地组件的状态
this.state.tables
。。我将
mainTables
传递给本地组件,并尝试在
componentDidMount
render
中设置状态。。因此,我正在寻找如何将
mainTables
存储到本地组件的状态。我能做什么