Last active
April 23, 2018 11:55
-
-
Save roryhow/a9a44981f7b3488adb225272539f836a 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 from 'react'; | |
import PropTypes from 'prop-types'; | |
import { connect } from 'react-redux'; | |
// Action creator | |
const toggleTodo = isChecked => ({ | |
type: 'TOGGLE_TODO', | |
isChecked, | |
}); | |
// Initial application state | |
const initialState = { | |
isChecked: false, | |
}; | |
// Reducer | |
const todo = (state, action) => { | |
switch (action.type) { | |
case 'TOGGLE_TODO': | |
return { | |
...state, | |
isChecked: action.isChecked, | |
}; | |
default: | |
return initialState; | |
} | |
}; | |
// A view that connects to the Reducer | |
export default function CheckboxContainer() { | |
const Todos = props => ( | |
<div> | |
<h2>This is my Todo: is it completed? {props.isChecked}</h2> | |
<input | |
type="checkbox" | |
value={props.isChecked} | |
onChange={() => toggleTodo(!props.isChecked)} | |
/> | |
</div> | |
); | |
Todos.propTypes = { | |
isChecked: PropTypes.boolean.isRequired, | |
}; | |
return connect( | |
state => ({ | |
todos: state.todos, | |
}), | |
{ toggleTodo }, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment