Reputation: 1341
I create an Boilerplate in react native. when i have parent and have to childs components and I want pasing state from child 1 to child 2
I've try send to parent then children2 but not working
This my Code Parent
class WashingPackageMotor extends Component {
render() {
return (
<View style={styles.containerMain}>
<Grid scrollable>
<Section>
<Block size="100%">
<ComponentWashingPackage />
</Block>
</Section>
<Section>
<Block size="100%">
<ComponentList />
</Block>
</Section>
</Grid>
<Grid>
<Section>
<Block size="100%">
<ComponentBottom />
</Block>
</Section>
</Grid>
</View>
}
);
}
}
Child 1 (Sibling one)
export default class ComponentList extends Component {
constructor(props) {
super(props)
this.state = {
value: 0,
amount: 0,
v0: 0,
v1: 0,
collapsed: false
// v5: 0,
// v6: 0
}
this.amount = 0
}
..........
..........
}
Child 2 (Sibling two)
class ComponentBottom extends Component {
render() {
return (
<Grid>
<View style={styles.bottomView}>
<View style={{ borderBottomColor: '#EEEEEE', borderBottomWidth:0.5}}/>
<Section>
<Block size="60%">
<Text style={styles.textHarga}>Total Harga</Text>
<Text style={styles.textPrice}>Rp 180.000</Text>
</Block>
<Block size="40%">
<ButtonProcessDisabled onPress={() => this.props.navigation.navigate('OrderConfirmation')}/>
</Block>
</Section>
</View>
</Grid>
)
}
}
And how to send state from Child 1 (Sibling 1) to Child 2 (Sibling2)
Upvotes: 0
Views: 49
Reputation: 1201
Steps to get the functionality you wish (not suggested though rather use redux or context):
Parent:
class WashingPackageMotor extends Component {
constructor(props) {
super(props);
this.state = {
amount: "",
}
}
update(amount) {
this.setState({amount});
}
render() {
return (
<View style={styles.containerMain}>
<Grid scrollable>
<Section>
<Block size="100%">
<ComponentWashingPackage />
</Block>
</Section>
<Section>
<Block size="100%">
<ComponentList
amount={this.state.amount}
updateAmount={amount => this.update(amount)}
/>
</Block>
</Section>
</Grid>
<Grid>
<Section>
<Block size="100%">
<ComponentBottom
amount={this.state.amount}
/>
</Block>
</Section>
</Grid>
</View>
}
);
}
}
Child1:
whenever you want to update the state just call the function this.props.updateAmount(valueHere);
export default class ComponentList extends Component {
constructor(props) {
super(props)
this.state = {
value: 0,
amount: this.props.amount,
v0: 0,
v1: 0,
collapsed: false
// v5: 0,
// v6: 0
}
this.amount = 0
}
..........
..........
}
child 2:
class ComponentBottom extends Component {
constructor(props) {
super(props);
this.state = {
amount: this.props.amount,
};
}
render() {
return (
<Grid>
<View style={styles.bottomView}>
<View style={{ borderBottomColor: '#EEEEEE', borderBottomWidth:0.5}}/>
<Section>
<Block size="60%">
<Text style={styles.textHarga}>Total Harga</Text>
<Text style={styles.textPrice}>Rp 180.000</Text>
</Block>
<Block size="40%">
<ButtonProcessDisabled onPress={() => this.props.navigation.navigate('OrderConfirmation')}/>
</Block>
</Section>
</View>
</Grid>
)
}
}
NOTE: You will need to use componentWillReceiveProps()
for react version older than 16.3 and for above you will need to use getDerivedStateFromProps
to update the state based on the updated props.
Upvotes: 1