我有一個容器「HomeIndexView」和組件「表」 而且我有全局和本地組件狀態表。如何存儲和訂閱組件狀態到Redux存儲?
全局表的狀態是像下面,
const initialState = {
allTables: [],
showForm: false,
fetching: true,
formErrors: null,
};
和局部表的組件的狀態是像下面,
componentWillMount() {
this.setInitialState();
}
setInitialState() {
this.setState({ tableBusy: false });
}
當,在HomeIndexView用戶登錄,它示出了從數據的所有表通過獲取基地。 所以我想要做的是將本地組件狀態連接到redux store,以便當它將state false更改爲true時,它會更改表的背景顏色。我應該如何將本地狀態連接到Redux商店?我應該爲本地組件的狀態創建單獨的縮減器和操作?
在此先感謝
--edit 1
import Table from '../../components/tables/table';
我進口LocalComponent(表),以HomeIndexView顯現。 在我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>
);
}
你說的 '全球性' 的狀態呢?你的意思是你創建了一個Redux商店,或者它只是你定義的'initialState'對象?如果是這樣,那裏。你能詳細說明上述狀態所在的組件層次結構嗎? – Pineda
是的,我創建了一個Redux存儲和'const initialState = {'是表組件的全局狀態。組件層次結構是Authenticated container> HomeIndexView> Table Component –
對於本地組件,我創建是因爲我希望數據庫中的每個表都具有tableBusy的狀態.. –