現在、Reactと、Reduxを使って情報を更新する機能を実装しています。
親要素の中で、handleSubmit
を使った時にはちゃんと動いたのですが、子要素の中で実行しようとすると反応がありません。目的としては、下記コードのなかでonSubmit()
を実行することになります。
src/components/ItemForm.js
JavaScript
1class ItemForm extends Component { 2 constructor(props) { 3 super(props); 4 } 5 6 componentDidMount() { 7 { id } = this.props; 8 this.props.fetchItem(id); 9 } 10 11 renderField = field => { 12 const { input } = field; 13 return ( 14 <label>アイテム</label> 15 <textarea {...input} placeholder={"内容を入力してください"} rows="6" /> 16 ); 17 }; 18 19 onSubmit = values => { 20 const { item } = this.props; 21 this.props.updateItem(item, values); 22 }; 23 24 render() { 25 const { handleSubmit, item, placeholder } = this.props; 26 return ( 27 <form 28 onSubmit={handleSubmit(this.onSubmit)} 29 > 30 <Field 31 label={title} 32 name="content" 33 value={item.content} 34 placeholder={placeholder} 35 component={this.renderField} 36 /> 37 <div className="formBtn"> 38 <button type="submit" className="btn btnPrimary"> 39 Save Changes 40 </button> 41 </div> 42 </form> 43 ); 44 } 45} 46 47const validate = values => { 48 const errors = {}; 49 if (!values.content) errors.content = "内容を入力してください"; 50 return errors; 51}; 52 53const mapDispatchToProps = { fetchItem, updateItem }; 54 55function mapStateToProps({ items }) { 56 return { 57 item: items, 58 initialValues: { 59 content: items.content 60 } 61 }; 62} 63 64export default connect( 65 mapStateToProps, 66 mapDispatchToProps 67)( 68 reduxForm({ validate, form: "itemForm", enableReinitialize: true })( 69 ItemForm 70 ) 71);
src/components/Item.js
class Item extends Component { constructor(props) { super(props); } render() { return ( <div> <div> <ItemForm id={1} /> <ItemForm id={2} /> </div> </div> ); } } export default Item;
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/17 01:36
2020/01/17 01:41
2020/01/17 01:42