I'm unable to get my component to re-render once I've called an action and updated my store with my reducer. The actual problem is that I cannot get my modal component to appear once I've clicked a button.
The state updates properly. I'm able to see that the boolean value I have in the store is changing from false to true, but it's not updating my component with the new information. Here's some code below:
// 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;
}
};
What seems to happen is the store is updated with showModal: true, but it doesn't translate to the view. The code above is just an example because the project is pretty large and overwhelming. I have other pieces in Redux working just fine, for some reason this is not working for me.
Here's a short video on what's happening in my live app It seems like the state changes, but doesn't update the view until I do something like try and scroll up on the FlatList on that page.