Ich muss einen Wert übergeben index
von Eltern zu Kind. Sowohl Eltern als auch Kind benötigen eine Funktion zum Überarbeiten index
. Wenn Eltern index
überarbeiten, kann Kind das Update nicht erhalten. Kann ich etwas reparieren?React: übergeben Wert von übergeordnet nach Kind funktioniert nicht
Elternklasse:
constructor(props) {
super(props);
this.state = {
index: 0
}
}
parentFunction(dir) {
this.setState({
index: 10
});
}
render() {
return(
<Child index={this.state.index}/>
);}
Child:
constructor(props) {
super(props);
this.state = {
index: this.props.index
};
}
childFunction(dir) {
this.setState({
index: this.props.index+1
});
}
render() {
return(
<div>{this.state.index}</div>
);}
Statt "this.props.index" in der Kindklasse, wie wäre es dann mit "props.index"? – Andy
nope.this ist nicht das Problem – ewrwerwe
Ich bin mir ziemlich sicher, dass es mindestens _Teil des Problems ist. – Andy