Last active
August 15, 2018 20:16
-
-
Save mariowise/c2dd6e5135118af30dad8a0eea260225 to your computer and use it in GitHub Desktop.
React States and Props
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import './App.css'; | |
import BrandsApi from './api/brand' | |
import ModelsApi from './api/model' | |
class App extends Component { | |
state = { | |
brands: [], | |
models: [], | |
} | |
componentDidMount() { | |
this.setState({ brands: BrandsApi.getAll() }) | |
} | |
onChangeBrand(e) { | |
let brand_id = e.target.value | |
this.setState({ brand_id, models: ModelsApi.getAllByBrandId(brand_id) }) | |
} | |
render() { | |
return ( | |
<div className="form"> | |
<div>Make:</div> | |
<select value={this.state.brand_id} onChange={this.onChangeBrand.bind(this)}> | |
{this.state.brands.map((brand, index) => <option key={index} value={brand.id}>{brand.name}</option>)} | |
</select> | |
<div>Model:</div> | |
<select value={this.state.model_id} onChange={e => this.setState({ model_id: e.target.value })}> | |
{this.state.models.map((model, index) => <option key={index} value={model.id}>{model.name}</option>)} | |
</select> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment