Last active
April 4, 2020 05:40
-
-
Save adriaanbd/6ce21b2417abec768fb0f0c7e6643151 to your computer and use it in GitHub Desktop.
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, { Component } from 'react'; | |
import { createStore } from 'redux' | |
import { render } from 'react-dom'; | |
import './style.css'; | |
const defaultState = { checked: false } | |
const reducer = (state = defaultState, action) => { | |
switch(action.type) { | |
case 'TOGGLE': | |
return { ...state, checked: !state.checked }; | |
} | |
return state; | |
} | |
const store = createStore(reducer); | |
class App extends Component { | |
constructor() { | |
super(); | |
this.state = {}; | |
} | |
componentWillMount() { | |
store.subscribe(() => this.setState(store.getState())) | |
} | |
render() { | |
const handleClick = () => store.dispatch({type: 'TOGGLE'}) | |
return ( | |
<div> | |
<h1>To-dos</h1> | |
<div> | |
Redux | |
<input | |
type="checkbox" | |
checked={!!this.state.checked} | |
onClick={handleClick}/> | |
</div> | |
{ | |
this.state.checked ? (<h2>Checked!</h2>) : null | |
} | |
</div> | |
); | |
} | |
} | |
render(<App />, document.getElementById('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment