2) Build Static version in react(no state, state for interactivity)
3) Identify The Minimal (but complete) Representation Of UI State(calculate computed terms on render and remember if its passed in as props then its the parent's job to change it)
4) Identify where state should live
5) Add Inverse Data Flow, pass update fts to lower components
Data Flow
Data flows down from parent to child, so if multiple components have a shared state put it in the closest common ancestor
Notice if we rerender a parent component with setState() all the children get rerendered (if needed?)
If something can be derived from either props or state, it probably shouldn’t be in the state.
Thinking about React
Data flows down from parent to child
Try to get small, reusable components
Components take props and return react component
Passing props for specialization is equivalent of composition/inheritance (i.e create WelcomeDialog by passing in specific props to Dialog)
can even pass other components in props and choose what to do with it
import React from"react"; //jsx support//Functional component - Good style when no need for advanced stufffunctionWelcome(props) {return <h1>Hello, {props.name}</h1>;}//has constructor, state&lifecycle stuffclassWelcomeextendsReact.Component {render() {return <h1>Hello, {this.props.name}</h1>; }}
Using Components
If you have user defined component(capitalized) in JSX, passes in props
constelement= <Welcomename="Sara" />;//ORconstx="Sara";constelement= <Welcomename={x} />//renders as: Hello, Sara
Passing in props to children
<Component {...this.props} more="values" />
Example
Lets say we are trying to make a F & C temp indicator
Temperature Input for F or C
classTemperatureInputextendsReact.Component {constructor(props) {super(props);this.handleChange =this.handleChange.bind(this); }handleChange(e) {this.props.onTemperatureChange(e.target.value); } //notice call ft from propsrender() {consttemperature=this.props.temperature;constscale=this.props.scale; //notice dynamic based on propsreturn ( <fieldset> <legend>Enter temperature in {scaleNames[scale]}:</legend> <inputvalue={temperature}onChange={this.handleChange} /> </fieldset> ); }}