Skip to content

Instantly share code, notes, and snippets.

@tomauty
Last active November 11, 2016 02:20
Show Gist options
  • Save tomauty/9745c8cf0ff85af573e0351bd1c32d50 to your computer and use it in GitHub Desktop.
Save tomauty/9745c8cf0ff85af573e0351bd1c32d50 to your computer and use it in GitHub Desktop.
higher order component / state sharing example
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