tor*_*eff 74 javascript reactjs
我有两个组件: Parent Component,我想从中更改子组件的状态:
class ParentComponent extends Component {
toggleChildMenu() {
?????????
}
render() {
return (
<div>
<button onClick={toggleChildMenu.bind(this)}>
Toggle Menu from Parent
</button>
<ChildComponent />
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
和儿童组成部分:
class ChildComponent extends Component {
constructor(props) {
super(props);
this.state = {
open: false;
}
}
toggleMenu() {
this.setState({
open: !this.state.open
});
}
render() {
return (
<Drawer open={this.state.open}/>
);
}
}
Run Code Online (Sandbox Code Playgroud)
我需要从Parent Component 更改Child Component的打开状态,或者在单击Parent Component中的Button时从Parent Component 调用Child Component的toggleMenu()?
Oli*_*ssé 105
应该在父组件中管理状态.您可以open通过添加属性将值传输到子组件.
class ParentComponent extends Component {
constructor(props) {
super(props);
this.state = {
open: false
};
this.toggleChildMenu = this.toggleChildMenu.bind(this);
}
toggleChildMenu() {
this.setState(state => ({
open: !state.open
}));
}
render() {
return (
<div>
<button onClick={this.toggleChildMenu}>
Toggle Menu from Parent
</button>
<ChildComponent open={this.state.open} />
</div>
);
}
}
class ChildComponent extends Component {
render() {
return (
<Drawer open={this.props.open}/>
);
}
}
Run Code Online (Sandbox Code Playgroud)
mig*_*ano 17
父组件可以管理将prop传递给子级的子状态,子级使用componentWillReceiveProps将状态转换为状态.
class ParentComponent extends Component {
state = { drawerOpen: false }
toggleChildMenu = () => {
this.setState({ drawerOpen: !this.state.drawerOpen })
}
render() {
return (
<div>
<button onClick={this.toggleChildMenu}>Toggle Menu from Parent</button>
<ChildComponent drawerOpen={this.state.drawerOpen} />
</div>
)
}
}
class ChildComponent extends Component {
constructor(props) {
super(props)
this.state = {
open: false
}
}
componentWillReceiveProps(props) {
this.setState({ open: props.drawerOpen })
}
toggleMenu() {
this.setState({
open: !this.state.open
})
}
render() {
return <Drawer open={this.state.open} />
}
}
Run Code Online (Sandbox Code Playgroud)
Jai*_*son 14
上面的答案对我来说部分正确,但在我的场景中,我想将值设置为一个状态,因为我已经使用该值来显示/切换模态.所以我用过如下.希望它会帮助某人.
class Child extends React.Component {
state = {
visible:false
};
handleCancel = (e) => {
e.preventDefault();
this.setState({ visible: false });
};
componentDidMount() {
this.props.onRef(this)
}
componentWillUnmount() {
this.props.onRef(undefined)
}
method() {
this.setState({ visible: true });
}
render() {
return (<Modal title="My title?" visible={this.state.visible} onCancel={this.handleCancel}>
{"Content"}
</Modal>)
}
}
class Parent extends React.Component {
onClick = () => {
this.child.method() // do stuff
}
render() {
return (
<div>
<Child onRef={ref => (this.child = ref)} />
<button onClick={this.onClick}>Child.method()</button>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
参考 - https://github.com/kriasoft/react-starter-kit/issues/909#issuecomment-252969542
您可以使用 createRef 从父组件更改子组件的状态。这是所有步骤。
创建一个方法来更改子组件中的状态。
2 - 使用 React.createRef() 在父组件中为子组件创建引用。
3 - 使用 ref={} 将引用附加到子组件。
4 - 使用 this.yor-reference.current.method 调用子组件方法。
父组件
class ParentComponent extends Component {
constructor()
{
this.changeChild=React.createRef()
}
render() {
return (
<div>
<button onClick={this.changeChild.current.toggleMenu()}>
Toggle Menu from Parent
</button>
<ChildComponent ref={this.changeChild} />
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
子组件
class ChildComponent extends Component {
constructor(props) {
super(props);
this.state = {
open: false;
}
}
toggleMenu=() => {
this.setState({
open: !this.state.open
});
}
render() {
return (
<Drawer open={this.state.open}/>
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
99445 次 |
| 最近记录: |