Skip to content

Instantly share code, notes, and snippets.

@thevishnupradeep
Created August 16, 2019 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thevishnupradeep/757eebe092544769328e6dc7f3bc4a56 to your computer and use it in GitHub Desktop.
Save thevishnupradeep/757eebe092544769328e6dc7f3bc4a56 to your computer and use it in GitHub Desktop.
/*********************** Imports ***********************/
import React from 'react';
import { connect } from 'react-redux';
import { useDispatch } from 'react-redux';
/********************* End Imports *********************/
/*********************** Toggle Function ***********************/
const Toggle = (props) => {
const isOpen = useSelector(state => state.isOpen);
const dispatch = useDispatch();
const toggleOpenState = () => {
dispatch({ type: "TOGGLE_STATE" });
}
return (
<div>
<Button onClick={toggleOpenState} name={isOpen ? "Close" : "Open"} />
</div>
);
};
/********************** End Toggle Function *******************/
export default Toggle;
/********************** Reducer Function *******************/
const isOpen = (state = false, action) => {
if (action.type === "TOGGLE_STATE") {
return !state;
}
return state;
}
/********************** End Reducer Function *******************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment