render()を行う際にpropsの値をsetStateしたいのですが。うまくいきません。。。
今回自身が行いたいこと
編集ホームの作成
js
1import React, { Component } from 'react'; 2import { Redirect } from 'react-router-dom'; 3 4import TitleField from './form/TitleField'; 5import SubmitButton from './form/SubmitButton'; 6 7import { Paper, Grid, Typography } from '@material-ui/core'; 8 9class editProduct extends Component{ 10 11 constructor(props){ 12 super(props); 13 this.state = { 14 data: { 15 title: this.props.title 16 } 17 }; 18 this.checkValue = this.checkValue.bind(this); 19 this.handleSubmit = this.handleSubmit.bind(this); 20 } 21 22 checkValue(type, value){ 23 const data = { 24 title: this.state.data.title 25 }; 26 data[type] = value; 27 this.setState({data: data}); 28 } 29 30 handleSubmit(event){ 31 const {title} = this.state.data; 32 this.props.onPut(event, this.props.match.params.id, title); 33 } 34 35 render(){ 36 if(this.props.redirect) { 37 return <Redirect to="/"/> 38 } 39 40 41 //productをこの画面にレンダリングした瞬間setStateしたい 42 const { product } = this.props; 43 44 //こいつを出したい 45 // const { title } = this.state.data; 46 return( 47 <React.Fragment> 48 <Paper> 49 <Typography variant="h5">Edit Product</Typography> 50 <Grid> 51 <Grid> 52 <TitleField onCheckValue={this.checkValue.bind(this)} title={product.title}/> 53 </Grid> 54 <Grid> 55 <SubmitButton onSubmit={this.handleSubmit.bind(this)}/> 56 </Grid> 57 </Grid> 58 </Paper> 59 </React.Fragment> 60 ); 61 } 62 63} 64export default editProduct; 65 66 67//inputFormについて 68import React from 'react'; 69import { Input } from '@material-ui/core'; 70 71const TitleField = (props) => { 72 const handleCheckValue = (event) => { 73 const type = event.target.name; 74 const value = event.target.value; 75 props.onCheckValue(type, value, event); 76 } 77 78 return( 79 <Input 80 value={props.title} 81 onChange={handleCheckValue} 82 /> 83 ); 84} 85 86export default TitleField;
現状だと、inputタグに打ち込むことができません。
ご助力よろしくお願いいたします。。。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。