Ben*_*ins 8 reactjs react-native redux react-redux
我总是使用react-redux connect配置道具,但我需要使用react Component来使用生命周期方法.我注意到我从商店抓起的道具似乎是静态的,并且随着商店更新它们不会更新.
码:
class AlertModal extends Component {
title
isOpen
message
componentDidMount() {
const { store } = this.context
const state = store.getState()
console.log('state', state)
console.log('store', store)
this.unsubscribe = store.subscribe(() => this.forceUpdate())
this.title = state.get('alertModal').get('alertModalTitle')
this.isOpen = state.get('alertModal').get('isAlertModalOpen')
this.message = state.get('alertModal').get('alertModalMessage')
this.forceUpdate()
}
componentWillUnmount() {
this.unsubscribe()
}
updateAlertModalMessage(message) {
this.context.store.dispatch(updateAlertModalMessage(message))
}
updateAlertModalTitle(title) {
this.context.store.dispatch(updateAlertModalTitle(title))
}
updateAlertModalIsOpen(isOpen) {
this.context.store.dispatch(updateAlertModalIsOpen(isOpen))
}
render() {
console.log('AlertModal rendered')
console.log('AlertModal open', this.isOpen) <======= stays true when in store it is false
return (
<View
Run Code Online (Sandbox Code Playgroud)
如何设置title,isOpen以及message使它们反映在任何时候都存储值?
Rav*_*ala 10
它应该是这样的.在您的确认组件中:
const mapStateToProps = (state) => {
return { modalActive: state.confirmation.modalActive };
}
export default connect(mapStateToProps)(Confirmation);
Run Code Online (Sandbox Code Playgroud)
在reducer索引文件中,应该是这样的:
const rootReducer = combineReducers({
confirmation: ConfirmationReducer
});
Run Code Online (Sandbox Code Playgroud)
我相信你有自己的reducer文件,名为ConfirmationReducer.它应该是这样的.
import { ON_CONFIRM } from '../actions';
const INITIAL_STATE = {modalActive: true};
export default function(state = INITIAL_STATE, action) {
console.log(action);
switch (action.type) {
case ON_CONFIRM:
return { ...state, modalActive: action.payload };
}
return state;
}
Run Code Online (Sandbox Code Playgroud)
确保编写自己的动作创建者以创建具有上述类型和布尔类型的相关有效负载的动作.
最后,您应该可以从Confirmation组件中的商店访问该属性,如下所示:
{this.props.modalActive}
Run Code Online (Sandbox Code Playgroud)
您尚未发布完整的代码,因此很难为确切的方案提供解决方案.希望这可以帮助.快乐的编码!
| 归档时间: |
|
| 查看次数: |
14162 次 |
| 最近记录: |