I have a component which i am importing into my App.js file and trying to set the value and an onChange method which i am passing down as props but not sure why that isn't working. Here is what the structure looks like:-
This is my app.js file:-
import React, { Component } from 'react';
import './App.css';
import Nested from './Nested.js'
class App extends Component {
state = {
childState: {
title: 'New Title',
}
}
updateTitle = (e) => {
const rows = [...this.state.childState];
rows.title = e.target.value
this.setState({
rows,
});
}
render() {
return (
<div className="App">
<Nested
title = { this.state.childState.title }
updateTitle = {this.updateTitle.bind(this) } />
</div>
);
}
}
export default App;
And this is my Nested Component:-
import React from 'react'
const Nested = (props) => {
return (
<div>
<input value = { props.title } onChange = { props.updateTitle }></input>
</div>
)
}
export default Nested;
If someone can point out what i am doing wrong in the above updateTitle, i'll appreciate it. Thank you.
rowsand where you are using this?updateTitle = (e) => { const rows = this.state.childState; rows.title = e.target.value this.setState({ childState: rows }); }rowswhich points tochildState. See my answer