[英]How can I store and subscribe component state to redux store?
I have a container "HomeIndexView" and component "Table" And I have both global and local component state of table. 我有一个容器“HomeIndexView”和组件“表”我有表的全局和本地组件状态。
Global table states are like below, 全局表状态如下所示,
const initialState = {
allTables: [],
showForm: false,
fetching: true,
formErrors: null,
};
and local component state of table is like below, 表的局部组件状态如下所示,
componentWillMount() {
this.setInitialState();
}
setInitialState() {
this.setState({ tableBusy: false });
}
When a user logs in, in HomeIndexView, it shows all tables from data base through fetching. 当用户登录时,在HomeIndexView中,它通过提取显示数据库中的所有表。 So what I want to do is that connecting local component state to redux store so that when it changes state false to true, it changes background color of table.
所以我想要做的是将本地组件状态连接到redux存储,以便当它将状态false更改为true时,它会更改表的背景颜色。 How should I connect local state to redux store?
我应该如何将本地状态连接到redux商店? and should I create separate reducer and action for the local component's state?
我应该为本地组件的状态创建单独的reducer和action吗?
Thanks in advance 提前致谢
--EDIT 1 --EDIT 1
import Table from '../../components/tables/table';
I am importing LocalComponent (Table) to HomeIndexView to show. 我正在导入LocalComponent(表)到HomeIndexView来显示。 In my HomeIndexView, it renders all tables from database,
在我的HomeIndexView中,它从数据库中呈现所有表,
_renderAllTables() {
const { fetching } = this.props;
let content = false;
if(!fetching) {
content = (
<div className="tables-wrapper">
{::this._renderTables(this.props.tables.allTables)}
</div>
);
}
return (
<section>
<header className="view-header">
<h3>All Tables</h3>
</header>
{content}
</section>
);
}
_renderTables(tables) {
return tables.map((table) => {
return <Table
key={table.id}
dispatch={this.props.dispatch}
{...table} />;
});
}
render() {
return (
<div className="view-container tables index">
{::this._renderAllTables()}
</div>
);
}
The 'react-redux'
library contains binding methods between React and Redux. 'react-redux'
库包含React和Redux之间的绑定方法。 If you haven't done so already, I really recommend checking out Dan Abramov's: 'Getting into Redux' series of videos. 如果您还没有这样做,我真的建议您查看Dan Abramov的: “进入Redux”系列视频。
He goes into a good amount of detail about how to build a working Redux application from scratch and then how to do the same in conjunction with React (again from scratch). 他详细介绍了如何从头开始构建一个可运行的Redux应用程序,然后如何与React一起完成同样的工作(再次从头开始)。
He finalises on the use of the 'react-redux'
helper library to make wiring up React with Redux easier. 他最终确定了使用
'react-redux'
辅助库来'react-redux'
React与Redux的连接。
The resulting solution for you would be to: 最终的解决方案是:
Use the connect
method in Redux to create a Redux container component (just a term for a React component with Redux bindings) 使用Redux中的
connect
方法创建Redux容器组件(只是具有Redux绑定的React组件的术语)
mapStateToProps
receives updates on the current state of the store which you can map to the target components props
. mapStateToProps
接收有关商店当前状态的更新,您可以将其映射到目标组件props
。 Yo'd use this to get the current state of the store for use in your component 你会用它来获取商店的当前状态,以便在你的组件中使用
mapDispatchToProps
which gets the store's dispatch action which you can use to bind action creators to (to update the store). mapDispatchToProps
获取商店的调度操作,您可以使用该操作将操作创建者绑定到(以更新商店)。 You'd use this to connect the action creators that update the state of your store. 您可以使用它来连接更新商店状态的动作创建者。
I assume you've already setup your reducers and actions. 我假设你已经设置了减速器和动作。 Now the only thing you need to do is dispatch an action from your
LocalComponent
. 现在,您唯一需要做的就是从
LocalComponent
调度一个动作。
Lets say you've a method called toggleTableState
for updating the state tableBusy
of your LocalComponent
. 假设您有一个名为
toggleTableState
的方法来更新tableBusy
的状态LocalComponent
。
LocalComponent.js LocalComponent.js
import React, { PureComponent, PropTypes } from 'react';
import { connect } from 'react-redux';
import * as actions from './actions`;
@connect(
(state, props) => ({
isTableBusy: state.isTableBusy,
}),
{
toggleTableGlobalState: actions.toggleTableGlobalState,
})
export default class LocalComponent extends PureComponent {
static propTypes = {
toggleTableGlobalState: PropTypes.func,
isTableBusy: PropTypes.bool,
}
...
toggleTableState() {
this.setState({ tableBusy: !this.state.tableBusy });
this.props.toggleTableGlobalState(!this.state.tableBusy);
}
...
}
actions.js actions.js
export const TOGGLE_TABLE_STATE = 'TOGGLE_TABLE_STATE';
export function toggleTableGlobalState(tableState) {
return { type: TOGGLE_TABLE_STATE, tableState };
}
reducer.js reducer.js
import { TOGGLE_TABLE_STATE } from './actions';
export default function reducer(state = initialState, action = {}) {
switch (action.type) {
...
case TOGGLE_TABLE_STATE:
return { ...state, isTableBusy: action.tableState };
break;
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.