Skip to content

Instantly share code, notes, and snippets.

@riyastir
Created August 12, 2021 11:16
Show Gist options
  • Save riyastir/216315b685bb1477574225993a5ee680 to your computer and use it in GitHub Desktop.
Save riyastir/216315b685bb1477574225993a5ee680 to your computer and use it in GitHub Desktop.
State issue - Redux
const [candidateParties, setCandidateParties] = useState([]);
const dispatch = useDispatch();
const currentParty = useSelector(currentSelectedParty);
useEffect(() => {
(async () => {
await fetch(`http://localhost:3000/api/v1/candidates/parties`)
.then((res) => res.json())
.then((result) => {
setCandidateParties(result.parties);
});
})();
}, []);
const dispatchNewParty = (party) => {
dispatch(changeParty(party));
};
return ({
candidateParties.map((data, key) => {
return ( <
button key = {
key
}
type = "button"
onClick = {
() => {
dispatchNewParty(data);
}
}
data - filter = ".blue"
className = {
currentParty === data ?
"control mixitup-control-active" :
"control"
} >
{
data
} </button>
);
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment