lundi 19 juin 2017

React Native - Modify a prop within the same component

I have simple questions:

  1. Is it possible to modify a prop (not state) within a React Native component (not from parent component)?

  2. Following [1], if I want to modify a prop within the same component, how can I achieve that (or workarounds if answer in [1] is No)?

  3. If I have the following:

//Parent

render(){
  return <View><ChildComponent propA={this.state.propA} /></View>
}

triggerChangeInParent(val) {
  this.setState({
    propA: val
  });
}

//Child (ChildComponent)

render() {
  return <View><Text>{this.props.propA}</Text></View>
}

triggerChangeInChild(val) {
  //To set props.propA here
}

Both parent and child components are allowed to modify "propA". Whoever triggers the latest will have its value taken precedence for modifying "propA" (eg if "triggerChangeInChild" is triggered after "triggerChangeInParent", then "val" from "triggerChangeInChild" will be used for "propA".

My question is, how do we achieve that (what possible solutions/alternatives to solve this problem)? What is the best practice/pattern?

Thanks!

Aucun commentaire:

Enregistrer un commentaire