【发布时间】:2018-10-08 22:11:58
【问题描述】:
我在我的应用程序中使用 redux。只有一种状态被定义。我可以更改状态并渲染屏幕。但是每当我更改状态道具时,我都无法重新加载屏幕。
代码:
action-types.js
export const SET_NOTIFICATION = "SET_NOTIFICATION";
action.js
import {
SET_NOTIFICATION,
} from "./action-types";
let initialState = {
notyIndex: 0,
};
export const setNotyIndex = (notyIndex) => ({type: SET_NOTIFICATION, notyIndex});
reducer.js
import {
SET_NOTIFICATION,
} from "./action-types";
let initialState = {
notyIndex: 0,
};
export default reducer = (state = initialState, action) => {
switch (action.type) {
case SET_NOTIFICATION:
return Object.assign({}, state, {notyIndex: action.notyIndex});
break;
default:
return initialState;
break;
}
};
我连接 redux 如下。 DashBoard.js
import { setNotyIndex } from "./action";
import {connect} from "react-redux"
********* 生命周期开始 ************
componentWillMount(){
console.log('Call update');
console.log('Index is',this.props.notyIndex);
}
shouldComponentUpdate=()=>{
return true
}
componentDidUpdate=(prevProps, prevState, snapshot)=>{
console.log('Call update');
console.log('Index is',this.props.notyIndex);
}
componentDidMount() {
console.log('Call update');
console.log('Index is',this.props.notyIndex);
}
*********生命周期结束************
const mapDispatchToProps = (dispatch) => {
return {
setNotyIndex: (notyIndex) => dispatch(setNotyIndex(notyIndex)),
}
};
const mapStateToProps = (state) => {
if (state === undefined) {
return {};
}
return {
notyIndex: state.notyIndex,
}
};
connect(mapStateToProps, mapDispatchToProps)(DashBoard);
值设置为。
setNotyIndex(1);
- 如上代码设置值后没有调用生命周期方法。
谢谢。
【问题讨论】:
-
请添加您的组件代码
-
@MotiKorets 实际上我已经在我的代码中添加了生命周期的所有方法,没有一个方法被调用。
-
他的意思是你调用
setNotyIndex的组件。setNotyIndexaction 或SET_NOTIFICATIONreducer 是否被调用? -
@MotiKorets 你可以看到我的代码中使用的生命周期方法。
-
@riwu 我在按钮事件中调用了 setNotyIndex。
标签: reactjs react-native redux react-redux