Skip to content

Instantly share code, notes, and snippets.

@adriaanbd
Last active April 4, 2020 05:40
Show Gist options
  • Save adriaanbd/6ce21b2417abec768fb0f0c7e6643151 to your computer and use it in GitHub Desktop.
Save adriaanbd/6ce21b2417abec768fb0f0c7e6643151 to your computer and use it in GitHub Desktop.
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&nbsp;
<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