Last active
November 11, 2016 02:20
-
-
Save tomauty/9745c8cf0ff85af573e0351bd1c32d50 to your computer and use it in GitHub Desktop.
higher order component / state sharing example
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
interface IComponentAProps { | |
fromB: string; | |
onChange: (t: string) => void; | |
} | |
class ComponentA extends Component<IComponentAProps, void> { | |
render() { | |
return ( | |
<View> | |
<Text>{this.props.fromB}</Text> | |
<TextInput onChangeText={this.props.onChange} /> | |
</View> | |
) | |
} | |
} | |
interface IComponentBProps { | |
fromA: string; | |
onChange: (t: string) => void; | |
} | |
class ComponentB extends Component<IComponentBProps, void> { | |
render() { | |
return ( | |
<View> | |
<Text>{this.props.fromA}</Text> | |
<TextInput onChangeText={this.props.onChange} /> | |
</View> | |
) | |
} | |
} | |
class CWrapper extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
aString: '', | |
bString: '', | |
} | |
} | |
render() { | |
return ( | |
<View> | |
<ComponentA onChange={(aChange: string) => this.setState({ aString: aChange })} fromB={this.state.bString} /> | |
<ComponentB onChange={(bChange: string) => this.setState({ bString: bChange })} fromA={this.state.aString} /> | |
</View> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment