Last active
September 27, 2017 22:36
-
-
Save overthemike/5481dab563c7f97f4151ab1c8c9cb4f1 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 {connect} from 'react-redux' | |
import {addFoo} from '../actions/foo' | |
class MyComponent extends Component { | |
static defaultProps = { | |
foos:[] | |
} | |
state = { | |
foo: '' | |
} | |
handleChange = (e) => { | |
this.setState({ | |
[e.target.name]:e.target.value | |
}) | |
} | |
handleSubmit = (e) => { | |
e.preventDefault() | |
// HERE IS WHERE MY QUESTION STARTS | |
// Why do this: | |
this.props.dispatch(addFoo(this.state.foo)) | |
// Instead of this: | |
addFoo(this.state.foo) | |
// and simply use store.dispatch() inside of of the addFoo | |
// function within the actions file? | |
} | |
render() { | |
return ( | |
<div> | |
<form onSubmit={this.handleSubmit}> | |
<input type="text" name="foo" value={this.state.foo} /> | |
<button type="submit">Add new foo</button> | |
</form> | |
<ul> | |
{this.props.foos.map(foo => ( | |
<li>{foo}</li> | |
))} | |
</ul> | |
</div> | |
) | |
} | |
} | |
function stateToProps (state) { | |
return { | |
foos: state.foos | |
} | |
} | |
export default connect(stateToProps)(MyComponent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment