In my component below, the input field loses focus after entering a character. When using the Chrome Inspector, it looks like the entire form is being re-displayed, not just the value attribute of the input field as you type.
I get no errors either from eslint or from Chrome Inspector.
Submitting the form itself works the same as the actual input field when it is located either in the rendering return or when importing as a separate component, but not in the way I encoded it below.
Why is this so?
Home Page Component
import React, { Component, PropTypes } from 'react'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import * as actionPost from '../redux/action/actionPost'; import InputText from './form/InputText'; import InputSubmit from './form/InputSubmit'; class _PostSingle extends Component { constructor(props, context) { super(props, context); this.state = { post: { title: '', }, }; this.onChange = this.onChange.bind(this); this.onSubmit = this.onSubmit.bind(this); } onChange(event) { this.setState({ post: { title: event.target.value, }, }); } onSubmit(event) { event.preventDefault(); this.props.actions.postCreate(this.state.post); this.setState({ post: { title: '', }, }); } render() { const onChange = this.onChange; const onSubmit = this.onSubmit; const valueTitle = this.state.post.title; const FormPostSingle = () => ( <form onSubmit={onSubmit}> <InputText name="title" label="Title" placeholder="Enter a title" onChange={onChange} value={valueTitle} /> <InputSubmit name="Save" /> </form> ); return ( <main id="main" role="main"> <div className="container-fluid"> <FormPostSingle /> </div> </main> ); } } _PostSingle.propTypes = { actions: PropTypes.objectOf(PropTypes.func).isRequired, }; function mapStateToProps(state) { return { posts: state.posts, }; } function mapDispatchToProps(dispatch) { return { actions: bindActionCreators(actionPost, dispatch), }; } export default connect(mapStateToProps, mapDispatchToProps)(_PostSingle);
Text input component
import React, { PropTypes } from 'react'; const InputText = ({ name, label, placeholder, onChange, value, error }) => { const fieldClass = 'form-control input-lg'; let wrapperClass = 'form-group'; if (error && error.length > 0) { wrapperClass += ' has-error'; } return ( <div className={wrapperClass}> <label htmlFor={name} className="sr-only">{label}</label> <input type="text" id={name} name={name} placeholder={placeholder} onChange={onChange} value={value} className={fieldClass} /> {error && <div className="alert alert-danger">{error}</div> } </div> ); }; InputText.propTypes = { name: PropTypes.string.isRequired, label: PropTypes.string.isRequired, placeholder: PropTypes.string.isRequired, onChange: PropTypes.func.isRequired, value: PropTypes.string, error: PropTypes.string, }; InputText.defaultProps = { value: null, error: null, }; export default InputText;
Submit Button Component
import React, { PropTypes } from 'react'; const InputSubmit = ({ name }) => { const fieldClass = 'btn btn-primary btn-lg'; return ( <input type="submit" value={name} className={fieldClass} /> ); }; InputSubmit.propTypes = { name: PropTypes.string, }; InputSubmit.defaultProps = { name: 'Submit', }; export default InputSubmit;