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
class Dumbo extends React.Component { | |
// static getDerivedStateFromProps(nextProps, prevState){ return prevState; } | |
state = { clothing : 'hat' }; | |
// componentDidMount() { } | |
// shouldComponentUpdate() { } | |
// getSnapshotBeforeUpdate() { } | |
// componentWillUnmount() { } | |
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
// JSX | |
const Dumbo = props => { | |
return <div>Hey, my name is Dumbo.</div>; | |
}; | |
// without JSX | |
const Dumbo = props => { | |
return React.createElement("div", null, `Hey, my name is Dumbo.`); | |
}; |
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
class Dumbo extends React.Component { | |
state = { clothing: null, firstChild: null, secondChild: null }; | |
handleState = () => { | |
this.setState({ | |
clothing: "jeans", | |
firstChild: "shoes", | |
secondChild: "hat" | |
}); | |
}; | |
render() { |
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
class DumbosParent extends React.Component { | |
state = { availableShoes: 6 }; | |
sellShoes = () => { | |
this.setState(({ availableShoes }, props) => ({ | |
availableShoes: availableShoes > 1 && availableShoes - 1 | |
})); | |
}; | |
render() { |
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
class DumbosParent extends React.Component { | |
state = { availableShoes: 6, lollipops: 2 }; | |
/* sellShoes */ | |
render() { | |
const { availableShoes, lollipops } = this.state; | |
return ( | |
<> | |
/* rest of code */ | |
<Dumbo availableShoes={availableShoes} lollipops={lollipops} /> | |
</> |
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
/* parent component */ | |
const { lollipops } = this.state; | |
return ( | |
<div> | |
<Dumbo availableShoes={availableShoes} candys={lollipops} /> | |
</div> | |
); | |
/* Dumbo component */ | |
const { candys } = this.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
/* Dumbo's parent component */ | |
const { lollipops } = this.state; | |
return ( | |
<div> | |
<Dumbo availableShoes={availableShoes} lollipops={lollipops} /> | |
</div> | |
); | |
/* Dumbo component */ | |
render () { |
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
// create Context | |
const initialAmountOfLollipops = 0; | |
const LollipopContext = React.createContext(initialAmountOfLollipops); | |
/* ... Dumbo's parent Component */ | |
const { lollipops } = this.state; | |
return ( | |
// instead of passing lollipops to Dumbo, we pass it to the Context.Provider | |
<CandyContext.Provider value={lollipops}> | |
<Dumbo availableShoes={availableShoes} /* lollipops={lollipops} */ /> |
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
/* HOC for wrapping the component with Context Provider */ | |
const connect = Component => { | |
return function WrappedComponent(props) { | |
return ( | |
<CandyContext.Consumer> /* pass lollipops as a prop */ | |
{value => <Component {...props} lollipops={value} />} | |
</CandyContext.Consumer> | |
); | |
}; | |
}; |
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
/* DumboParent.js */ | |
class DumbosParent extends React.Component { | |
state = { availableShoes: 6, lollipops: 9 }; | |
eatCandy = (quantity = 0) => { | |
this.setState(({ lollipops }) => ({ | |
lollipops: lollipops - quantity | |
})); | |
}; |
OlderNewer