I have a component which is supposed to update if a state from another component has been changed using redux, but it's not.
Inside mapStateToProps
the correct value is being returned on redux action.
import React, {Component} from "react";
import './DashboardContent.scss';
import * as entries from '../../../assets/demo.json';
import CategoryHeader from "./CategoryHeader/CategoryHeader";
import NoContent from "../../../shared/NoContent/NoContent";
import UnsortedList from "./UnsortedList/UnsortedList";
import {connect} from "react-redux";
class DashboardContent extends Component {
state = {
activeCategory: this.props.activeCategory
};
componentDidUpdate(prevProps, prevState, snapshot) {
console.log(this.props.activeCategory)
}
render() {
return (
<div>
...
</div>
)
}
}
const mapStateToProps = state => {
console.log(state);
return {
activeCategory: state.category
}
};
export default connect(mapStateToProps)(DashboardContent);
Dispatch inside of another component - when this is being executed, the state activeCategory
of first component shall become some value
:
dispatch(changeCategory('some value'))
Actions.js
// Action types
const CHANGE_CATEGORY = 'CHANGE_CATEGORY'
// Action creators
export const changeCategory = (category) => {
return {
type: CHANGE_CATEGORY,
category
}
}
Reducer.js
const initialState = {
activeCategory: 'all'
};
export const reducer = (state = initialState, action) => {
console.log('reducer', state, action);
if (action.type === 'CHANGE_CATEGORY') {
return action.category
}
return state;
};