Last active
March 30, 2019 13:24
-
-
Save alexandrzavalii/8e65639db7f49a6525ae77ee318ca6eb to your computer and use it in GitHub Desktop.
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 from "react"; | |
import FirstChild from "./FirstChild"; | |
import SecondChild from "./SecondChild"; | |
class DumbosParent extends React.Component { | |
state = { availableShoes: 6, lollipops: 9 }; | |
render() { | |
const { availableShoes, lollipops } = this.state; | |
return ( | |
<div> | |
<h1>I am Dumbos Father. I keep track of shoes and lollipops.</h1> | |
<Dumbo | |
availableShoes={availableShoes} | |
firstChild={<FirstChild lollipops={this.state.lollipops} />} | |
secondChild={<SecondChild />} | |
/> | |
<AnotherComponent availableShoes={this.state.availableShoes} /> | |
</div> | |
); | |
} | |
} | |
// Dumbo.js | |
class Dumbo extends React.Component { | |
state = { clothing: null, firstChild: null, secondChild: null }; | |
handleState = () => { | |
this.setState({ | |
clothing: "jeans", | |
firstChild: "shoes", | |
secondChild: "hat" | |
}); | |
}; | |
render() { | |
return ( | |
<div> | |
<h1>Hey, my name is Dumbo. </h1> | |
<p> I am wearing {this.state.clothing} today.</p> | |
<button onClick={this.handleState}>dress up</button> | |
/* --- we have a problem here, children also expect props from Dumbo. --- */ | |
{this.props.firstChild} | |
{this.props.secondChild} | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment