0
如何在調用React
組件的函數時給出該組件的變量?我有一個Parent
,通過Test
類到Child
組件,並且這個孩子想要改變Test
中的東西。從聲明爲變量的React組件調用函數
export class Parent extends React.Component {
render() {
let test = (<Test />);
return (<Child tester={test} />);
}
}
export class Child extends React.Component {
render() {
this.props.tester.setText("qwerty"); // how to invoke setText, setState or something like that?
return ({this.props.tester});
}
}
export class Test extends React.Component {
constructor(props) {
super(props);
this.state = {
text: this.props.text || ""
};
}
setText(text) {
this.setState({ text: text });
}
render() {
return (<div>{this.state.text}</div>);
}
}
你想在'Test'中改變什麼?你可以通過你想要改變的所有東西作爲道具並立即渲染。不需要功能。 –
我想在'Child'裏傳遞一些東西,其中'Test'只是從'Parent'傳遞的對象的引用。我不能簡單地'this.props.tester.text =「sth」' – Nickon