edit: For those looking for a solution, please look at my submitted solution below.
Show:
{" "}
<FilterLink filter="SHOW_ALL">
All
</FilterLink>
{", "}
<FilterLink filter="HIDE_DUPLICATES">
Duplicates
</FilterLink>
</p>
I'm writing a Redux app that will update the state when a checkbox is checked. Right now I'm working on getting the store to update correctly when the checkbox is checked.
The problem I'm encountering right now is I'm not properly dispatching the toggleDuplicates action from containers/GlossaryControls.js.
A copy of the code can be found here.
Thanks in advance for the help!
actions/actions.js
const toggleDuplicates = (filter) => {
return {
type: "TOGGLE_DUPLICATES",
filter
};
};
export default toggleDuplicates;
reducers/data.js
let words = [
{
"id": 0,
"english": "woof",
"french": "le woof"
},
{
"id": 1,
"english": "woof",
"french": "le woof"
}];
export default words;
reducers/toggleDuplicates.js
const toggleDuplicates = (state, action) => {
switch (action.type) {
case "TOGGLE_DUPLICATES":
return state; // duplicate removal logic will go here
default:
return state;
}
};
export default toggleDuplicates;
reducers/index.js
import {combineReducers} from "redux";
import words from "./data.js";
import toggleDuplicates from "./toggleDuplicates";
const allReducers = combineReducers({
words,
toggleDuplicates
});
export default allReducers;
containers/GlossaryControls
import React from "react";
import {connect} from "react-redux";
import toggleDuplicates from "../actions/actions";
let GlossaryControls = (props) => {
return (
<div>
<input
type="checkbox"
checked={this.props.toggleDuplicates}
onChange={toggleDuplicates}
/>
{" "}
Hide duplicates
</div>
);
};
const mapStateToProps = (state, ownProps) => {
return {
toggleDuplicates: ownProps.toggleDuplicates === state.toggleDuplicates
};
};
const mapDispatchToProps = (dispatch, ownProps) => {
return {
onClick: () => {
dispatch(toggleDuplicates(ownProps.toggleDuplicates))
}
};
};
const FilterDuplicates = connect (
mapStateToProps,
mapDispatchToProps
)(GlossaryControls);
export default FilterDuplicates;