【发布时间】:2019-01-28 15:51:03
【问题描述】:
一旦我调用了一个操作并使用我的 reducer 更新了我的商店,我就无法让我的组件重新渲染。实际的问题是,一旦我点击了一个按钮,我就无法让我的模态组件出现。
状态更新正确。我可以看到商店中的布尔值从false 更改为true,但它没有使用新信息更新我的组件。下面是一些代码:
// Home Page
import React, { Component } from 'react';
import ModalComponent from '../components/modal.component';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { toggleShowModal } from '../actions/modal-actions';
class HomePage extends Component {
state = {
// some values
showModal: false,
};
// added on edit
componentWillReceiveProps(nextProps) {
if (nextProps !== this.props) {
this.setState({
showModal: nextProps.showModal,
})
}
}
_toggleModalVisibility = () => {
// redux action
this.props.toggleShowModal();
}
render() {
<ModalComponent isVisible={this.state.showModal} />
}
}
const mapStateToProps = (state) => {
return {
showModal: state.showModal,
};
};
const mapDispatchToProps = (dispatch) => {
return bindActionCreators({ toggleShowModal }, dispatch);
};
export default connect(mapStateToProps, mapDispatchToProps)(HomePage);
// Actions
import { SHOW_MODAL } from './types';
export const toggleShowModal = () => dispatch => {
dispatch({
type: SHOW_MODAL,
showModal: true,
});
};
// Reducers (reducers are combined in another file and work fine)
import { SHOW_MODAL } from '../actions/types';
const initialState = {
showModal: false,
};
export const modalReducer = (state = initialState, action) => {
switch (action.type) {
case SHOW_MODAL:
return {
...state,
showModal: action.showModal,
};
default:
return state;
}
};
似乎发生的情况是商店已更新为showModal: true,但并未转换为视图。上面的代码只是一个示例,因为该项目非常庞大且势不可挡。我在 Redux 中的其他部分工作得很好,但由于某种原因这对我不起作用。
Here's a short video on what's happening in my live app 似乎状态发生了变化,但在我执行类似尝试并在该页面上的FlatList 上向上滚动之前不会更新视图。
【问题讨论】:
-
你用过 combinereducers 吗?
-
我是说你。有多个减速器?
-
@Sarmad 是的,我确实有一个组合减速器的文件。
标签: javascript reactjs react-native redux react-redux