Kay Ashaolu
<input type="text" value="I just typed this" />
import React from "react"; import ReactDOM from "react-dom"; class NameForm extends React.Component { constructor(props) { super(props); this.state = {value: ''}; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange(event) { this.setState({value: event.target.value.toUpperCase()}); } handleSubmit(event) { alert('A name was submitted: ' + this.state.value); event.preventDefault(); }
render() { return ( <form onSubmit={this.handleSubmit}> <label> Name: <input type="text" value={this.state.value} onChange={this.handleChange} /> </label> <input type="submit" value="Submit" /> </form> ); } } ReactDOM.render( <NameForm />, document.getElementById('root') );
import React from "react"; import ReactDOM from "react-dom"; class Reservation extends React.Component { constructor(props) { super(props); this.state = { isGoing: true, numberOfGuests: 2 }; this.handleInputChange = this.handleInputChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleInputChange(event) { const target = event.target; const value = target.type === 'checkbox' ? target.checked : target.value; const name = target.name; this.setState({ [name]: value }); }
handleSubmit(event) { alert(`A reservation was submitted: ${this.state.isGoing ? "We're going" : "Not going"} with ${this.state.numberOfGuests} guest(s)`); event.preventDefault(); } render() { return ( <form onSubmit={this.handleSubmit}> <label> Is going: <input name="isGoing" type="checkbox" checked={this.state.isGoing} onChange={this.handleInputChange} /> </label> <br />
<label> Number of guests: <select name="numberOfGuests" value={this.state.numberOfGuests} onChange={this.handleInputChange}> <option value="0">None</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> </label> <br /> <input type="submit" value="RSVP" /> </form> ); } } ReactDOM.render( <Reservation />, document.getElementById('root') );
import React from "react"; import ReactDOM from "react-dom"; class CityForm extends React.Component { constructor(props) { super(props); this.state = {city: 'berkeley', temp: 0}; this.handleInputChange = this.handleInputChange.bind(this); } getWeather(city) { let that = this; fetch(`http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=af578739923ac7f173a6054b24c606ea`) .then(function (response) { return response.json(); }).then(function (result) { that.setState({"temp":result.main.temp - 273.15, "city":city}) }); } componentDidMount() { this.getWeather(this.state.city); }
handleInputChange(event) { const target = event.target; const value = target.type === 'checkbox' ? target.checked : target.value; const name = target.name; this.getWeather(value); } render() { return ( <div> <form> <label> City: <select name="city" value={this.state.city} onChange={this.handleInputChange}> <option value="berkeley">Berkeley, CA</option> <option value="manhattan">New York, NY</option> <option value="chicago">Chicago, IL</option> </select> </label> </form>
<div>The temperature (in celsius) of {this.state.city} is {this.state.temp}</div> </div> ); } } ReactDOM.render( <CityForm />, document.getElementById('root') );