Created
November 21, 2019 23:38
-
-
Save shanebdavis/39f59f3f96869131c4274b88ab39a57a to your computer and use it in GitHub Desktop.
Toggle implemented with hooks-for-redux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import ReactDOM from "react-dom"; | |
import { applyMiddleware } from "redux"; | |
import { composeWithDevTools } from "redux-devtools-extension"; | |
import thunkMiddleware from "redux-thunk"; | |
import { Provider, useRedux, createStore, setStore } from "hooks-for-redux"; | |
import "./styles.css"; | |
// store.js | |
const store = setStore( | |
createStore({}, composeWithDevTools(applyMiddleware(thunkMiddleware))) | |
); | |
// toggleState.js | |
const [useToggle, { toggleSwitch }] = useRedux("toggle", false, { | |
toggleSwitch: state => !state | |
}); | |
// Toggle.js | |
const Toggle = () => { | |
const toggle = useToggle(); | |
return ( | |
<div> | |
<div>{JSON.stringify(toggle)}</div> | |
<input type="checkbox" value={toggle} onChange={() => toggleSwitch()} /> | |
</div> | |
); | |
}; | |
// index.js | |
const App = () => ( | |
<Provider> | |
<Toggle /> | |
</Provider> | |
); | |
const rootElement = document.getElementById("root"); | |
ReactDOM.render(<App />, rootElement); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment