React Native (with Redux) animate Switch programmatically

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>
        )
    }
}
+4
1

. - , , , .

, shouldComponentUpdate(),

:

. ​​ false, .

enter image description here

true, View Switch

enter image description here

, , .

state ( Switch), "Re-render" , :

Re-rendering result

:

var isActive = false; //global variable
class Test extends Component {
  constructor(props) {
    super(props);
    this.state={active:false, irrelevantState: true};
  }

  test(foo){
    console.log("Attempting to change", foo);
    isActive = foo;
  }

  shouldComponentUpdate(nextProps, nextState){
    if(this.state.active != nextState.active){//If the new state is different than the previous, stop rendering.
      this.test(nextState.active);
      return false;
    }else { //else, re-render everything
      return true;
    }
  }

  render(){
    console.log("Test re-render");
    return(
      <View style={{flex:1}}>
        <TouchableOpacity onPress={()=>{this.setState({active:!this.state.active})}} style={{flex:1, marginTop:20}}>
          <Text>Test Component</Text>
          <Switch value={isActive}/>
        </TouchableOpacity>
        <TouchableOpacity onPress={()=>{this.setState({active:true})}} style={{flex:1, marginTop:20}}>
          <Text>Test Component</Text>
          <Switch value={isActive}/>
        </TouchableOpacity>
        <TouchableOpacity style={{height:20}} onPress={()=>this.setState({irrelevantState:false})}>
          <Text> Re-render</Text>
        </TouchableOpacity>
      </View>
    )
  }
}

, this.state.active , .

0

Source: https://habr.com/ru/post/1671295/


All Articles