Төслийн App компонентийн кодыг харвал
class App extends Component {
state = {
cars: [
{ name: 'Toyata Prius', year: '2018' },
{ name: 'Toyata Camry', year: '2019' },
{ name: 'Ford', year: '2015' }
],
pageTitle: 'App компонент'
}
changeTitleHandler = (newTitle) => {
this.setState({
pageTitle: newTitle
})
}
ChangeHandle = (event) => {
this.setState({
pageTitle: event.target.value
})
}
render() {
const divStyle = {
textAlign: 'center'
}
const cars = this.state.cars
return (
<div style={divStyle}>
<h1>
{this.state.pageTitle}
</h1>
<input type="text" onChange={this.ChangeHandle} />
<button onClick={this.changeTitleHandler.bind(this, 'App өөрчлөлт')}>Гарчиг өөрчлөх</button>
<Car
name={cars[0].name}
year={cars[0].year}
onChangeTitle={this.changeTitleHandler.bind(this, cars[0].name)}
/>
<Car
name={cars[1].name}
year={cars[1].year}
onChangeTitle={() => this.changeTitleHandler(cars[1].name)}
/>
<Car
name={cars[2].name}
year={cars[2].year}
onChangeTitle={() => this.changeTitleHandler(cars[2].name)}
/>
</div>
)
}
}
болсон байгаа.