I have a component with a Switch component inside. All this component can be clicked. When you click on this component, it starts sending (which changes the repository of reducts) and forces this component to re-render. I do not need to redraw the entire component, but simply animate this Switch component in the correct state.
Something like that
<TouchableOpacity onPress={onPress}>
<Text>{this.props.title}</Text>
<Switch value={this.state.active}/>
</TouchableOpacity>
Any idea?
I find this rather common use case, and I know that I will get many similar scenarios, so I need this behavior, but I can not find any examples or solutions on the Internet. Maybe I just missed something.
thank
--- EDIT ---
. shouldComponentUpdate, willRecieveProps / , . LayoutAnimation. . this.setState() Switch.
this._switch._rctSwitch.setNativeProps({: nextProps.active}), ( - _rctSwitch).
class ViewWithSwitchInside extends React.Component {
constructor(props) {
super(props)
this.toggle = this.toggle.bind(this);
this.state = {
active: props.active
}
}
componentWillReceiveProps(nextProps) {
if (this.state.active != nextProps.active) {
this.setState({ active: nextProps.active })
}
}
toggle() {
let newValue = !this.state.active
this.setState({
active: newValue,
})
if (typeof this.props.onClick === 'function') {
this.props.onClick(newValue)
}
}
render() {
return (
<TouchableWithoutFeedback onPress={this.toggle}>
<View>
<Text>{this.props.title}</Text>
<Switch value={this.state.active} />
</View>
</TouchableWithoutFeedback>
)
}
}