I have a input, the user will entry text in the InputField
component (state will change, therefore).
[...]
constructor(props)
this.state = {text: ''}
this.onChange = (event) => {
this.setState({text: event.target.value})
}
[...]
render() {
return (<input type='text' onChange={this.onChange} textValue={this.state.text}/>)
}
I will pass this text
as props for the main component form. But it isn't working:
1st try
[...]
constructor(props)
this.state = {text: ''}
this.onChange = (event) => {
this.setState({text: event.target.value})
}
[...]
render() {
return(<input type='text' onChange={this.onChange} textValue={text.state.text}/>)
}
2nd try
I also tried this way: [...] constructor(props)
this.state = {text: ''}
this.onChange = (event) => {
this.setState({text: event.target.value})
this.props.textValue ({text: text})
}
[...]
render() {
return(<input type='text' onChange={this.onChange}/>)
}
And then I get the textValue
property, so I can send the input's value within the submit.
[...]
this.handleSubmit = () => {
let data = {text: this.props.data}
}
[...]
render() {
return(
<form onSubmit={this.handleSubmit} data={this.props.textValue}>
<InputField/>
<button type='submit'>Send</button>
</form>
)
}
Why am not I achieving what I want?
After researching the concepts behind props and state, I guess I should be doing the opposite and that's why it's getting so hard (maybe). Like, in the Form
I should be using state instead of props (and InputField
props instead of states, so handling the change not in InputField
but in Form
like this: https://stackoverflow.com/a/31756470 - I'm doing the opposite of the 'Parent with single child' example). I mixed up everything and now I'm confused where dit it all go wrong.
**Conceptual examples are more welcome than practical ones. My problem here is the theory behind this flow. **