I want to capture form values ββwhen the user submits the form. The React manual shows the following:
var CommentForm = React.createClass({ handleAuthorChange: function(e) { this.setState({author: e.target.value}); }, handleTextChange: function(e) { this.setState({text: e.target.value}); }, handleSubmit: function(e) { e.preventDefault(); var author = this.state.author.trim(); var text = this.state.text.trim(); if (!text || !author) { return; } render: function() { return ( <form className="commentForm" onSubmit={this.handleSubmit}>
This style pushes all state changes through the processing function to each input. If I were to continue working with Redux, I would add that I would add actions and action creators to the action, which seems like a lot of overhead.
Note that the handleSubmit argument is an event.
I came across the following kit to run React / Redux, which looks a lot easier to work with. It uses a combination of smart / container components and a silent / presentation component:
Container
@connect( () => ({}), {initialize}) export default class Survey extends Component { ... handleSubmit = (data) => { window.alert('Data submitted! ' + JSON.stringify(data)); this.props.initialize('survey', {}); } render() return ( <SurveyForm onSubmit={this.handleSubmit}/> ) ...
Presentation
class SurveyForm extends Component { static propTypes = { handleSubmit: PropTypes.func.isRequired, } render() { const { handleSubmit } = this.props; return ( <div> <form className="form-horizontal" onSubmit={handleSubmit}> ... } }
I donβt understand why the handler for the onSubmit form onSubmit accepts an event for its argument for the Facebook tutorial, but why the onSubmit accepts form data ... The starter kit uses the redux-form package, but I donβt see it directly affect the behavior this handler.
reactjs redux react-redux redux-form
Mister epic
source share