我结合使用reducers并在index.js中添加初始状态。
const store = createStore(Reducers, initialState);
和mapDispatchToProps
是这样的:
const mapDispatchToProps = (dispatch, ownProps)=>({
getAboutMeData: ()=> dispatch(getAboutMeData())
});
我的行为就是这样:
export const getAboutMeData = () => ({
type: GET_ABOUTME_DATA,
payload:{
basicInfo: {
name: "change data",
job: "change data",
hometown: "change data",
birthday: "change data",
degree: "change data",
livein: "change data",
avater: "change data",
school: "change data",
majorIn: "change data"
}
}
})
我在使用componentDidMount
时更新我的组件:
componentDidMount(){
const {getAboutMeData} =this.props;
getAboutMeData();
}
我的减速机是这样的:
export default function aboutMe(state= {}, action) {
switch(action.type) {
case GET_ABOUTME_DATA:
return action.payload
default:
return state
}
}
但是我的组件没有更新状态,我不知道为什么。
答案 0 :(得分:0)
组件仅使用道具或状态更改进行更新。 因此,调度操作和更新组件是无关紧要的。
您应该配置mapStateToProps,如下所示
const mapStateToProps = state => {
return {
aboutMe: state.aboutMe
};
}
const mapDispatchToProps = (dispatch, ownProps)=>({
getAboutMeData: ()=> dispatch(getAboutMeData())
});
export default connect(mapStateToProps, mapDispatchToProps)(Component)