Stateful comonent: has state property. Stateless component: does not have state property. Stateful comp passes its state down to a stateless component.
To access a prop: use this.props.name-of-prop
Parent is going to pass a prop to child. So parent is going to render a child! Is the only way for a component to pass props to another component. The component rendered y a different component must be included in an export statement.
Then the parent needs to import the child: import { ComponentName } from './Component'. THen parent cann pass its state to child. In return statement: child instance. Then render components.
A component should never update this.props! A React component should use props to store information that can be changed, but can only be changed by a different component.
A React component should use state to store information that the component itself can change.
To make a child component update its parent's state (pass event handler to stateless child component, child uses to update parents state):
Child compent updates parent's state, parent passes that state into sibling component. You will have one stateless component display info, and a different display stateless component offer the ability to change that info.
export class Child extends React.Component{
render(){
return <h1>Hey, my name is {this.props.name}!</h1>;
}
}
import {Child} from './Child';
class Parent extends React.Component {
constructor(props){
super(props);
this.state = {name: 'Frarthur'};
}
render(){
return <Child name={this.state.name}/>;
}
}
ReactDOM.render(<Parent />, document.getElementById('app'));
-- new function that can change this.state.name. Add to Parent class.
changeName(newName) {
this.setState({
name: newName
-- changeName should always refer to instance of Parent, even when we pass it to Child.
this.changeName = this.changeName.bind(this);
--inside Parent's render function, add attribute onChange = {this.changeName}
return <Child name={this.state.name} onChange={this.changeName}/>
});
-- inside child before render
handleChange(e) {
const name = e.target.value;
this.props.onChange(name);
}
-- bind to child
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
}
A stateful component class defines a function that calls this.setState.
The stateful component passes that function down to a stateless component.
That stateless component class defines a function that calls the passed-down function, and that can take an event object as an argument.
The stateless component class uses this new function as an event handler.
When an event is detected, the parent’s state updates. (A user selects a new dropdown menu item)
The stateful component class passes down its state, distinct from the ability to change its state, to a different stateless component.
That stateless component class receives the state and displays it.
An instance of the stateful component class is rendered. One stateless child component displays the state, and a different stateless child component displays a way to change the