Last active
September 20, 2018 17:29
-
-
Save ajcrites/a7824fc506b8ccd3415b6da57a8b421d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Example - prop drilling through nested rendering | |
const Name = ({ name }) => <Text>{name}</Text>; | |
const Block = ({ name }) => <View><Name name={name} /></View>; | |
const Bar = ({ name }) => <View><Block name={name} /></View>; | |
const Nav = ({ name }) => <View><Bar name={name} /></View>; | |
export const Profile = ({ name }) => <View><Nav name={name} /></View>; |
This file contains hidden or 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
// Example - handle prop drilling via context | |
const NameContext = React.createContext({ name }); | |
const Name = () => ( | |
<NameContext.Consumer> | |
{({ name }) => <Text>{name}</Text>} | |
</NameContext.Consumer> | |
); | |
const Block = () => <View><Name /></View>; | |
const Bar = () => <View><Block /></View>; | |
const Nav = () => <View><Bar /></View>; | |
export const Profile = ({ name }) => ( | |
<Name.Provider name={name}> | |
<View> | |
<Nav /> | |
</View> | |
</Name> | |
); |
This file contains hidden or 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
// Example - pass props via composition | |
export const Profile = ({ name }) => ( | |
<View> | |
<View testID="Nav"> | |
<View testID="Bar"> | |
<View testID="Block"> | |
<Text testID="Name">{name}</Text> | |
</View> | |
</View> | |
</View> | |
</View> | |
); |
This file contains hidden or 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
// Example - reusable components with composition | |
const Name = ({ name }) => <Text>{name}</Text>; | |
const Block = ({ children }) => <View>{children}</View>; | |
const Bar = ({ children }) => <View>{children}</View>; | |
const Nav = ({ children }) => <View>{children}</View>; | |
export const Profile = ({ name }) => ( | |
<View> | |
<Nav> | |
<Bar> | |
<Block> | |
<Name name={name} /> | |
</Block> | |
</Bar> | |
</Nav> | |
</View> | |
); |
This file contains hidden or 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
// Good(?) Example - compromise | |
export const NavBar = ({ children }) => ( | |
<View testID="Nav"> | |
<View testID="Bar"> | |
<View testID="Block"> | |
{children} | |
</View> | |
</View> | |
</View> | |
); | |
export const Profile = ({ name }) => ( | |
<View> | |
<NavBar> | |
<Text testID="Name">{name}</Text> | |
</NavBar> | |
</View> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment