0
我的店是下一個:有一些方法可以通過react和redux讀取reducer中的子狀態?
const initialState = {
all: {
"name": "",
"datas": {
"data1": {
"subdata1": [],
"subdata2": [],
},
"data2": {
"subdata1": [],
"subdata2": [],
},
}
}
}
我的下一個組件:
* All: main Component, map to Data with all.datas
* Data: show data1 or data2 or data3. map to Subdata with data1 and data2 ...
* Subdata: show list subdata1
我有一個終極版連接的子數據,和我想的是,減速,而不是返回一般狀態,返回,一個子數據狀態。
例如:
Subdata.js:
class Subdata extends React.Component {
// ....
}
function mapStateToProps(state, ownProps) {
return {
subdata: ownProps.subdata
};
}
function mapDispatchToProps(dispatch) {
return { actions: bindActionCreators(Actions, dispatch) }
}
export default connect(mapStateToProps, mapDispatchToProps)(Subdata);
reducers.js:
const subdata = (state = {}, action) => {
// I want that state return: subdata, in this case, []
// instead of all: {...}
const copy = JSON.parse(JSON.stringify(state));
switch(action.type) {
case "ADD_SUBDATA":
// In this moment: I need pass a lot of vars: data_id and subdata_id
my_subdata = state.datas[data_id][subdata_id]
// I want get directly the subdata
my_subdata = state.subdata
default:
return state;
}
}
這可能嗎?怎麼樣?
非常感謝。