[英]React js change child component's state from parent component
我有两个组件: 我想从中更改子组件的 state 的父组件:
class ParentComponent extends Component {
toggleChildMenu() {
?????????
}
render() {
return (
<div>
<button onClick={toggleChildMenu.bind(this)}>
Toggle Menu from Parent
</button>
<ChildComponent />
</div>
);
}
}
和子组件:
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}/>
);
}
}
我需要从父组件更改子组件的打开state,还是在单击父组件中的按钮时从父组件调用子组件的toggleMenu() ?
状态应该在父组件中管理。 您可以通过添加属性将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}/>
);
}
}
父组件可以管理将 prop 传递给子组件的子状态,子组件使用 componentWillReceiveProps 将这个 prop 转换为 state。
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} />
}
}
上面的答案对我来说部分正确,但在我的场景中,我想将该值设置为一个状态,因为我已使用该值来显示/切换模态。 所以我使用如下。 希望它会帮助某人。
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>
);
}
}
参考 - 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>
);
}
}
子组件
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}/>
);
}
}
您可以从父组件发送一个 prop 并在子组件中使用它,以便您将子组件的状态更改基于发送的 prop 更改,并且您可以通过在子组件中使用getDerivedStateFromProps来处理此问题。
如果您正在使用功能组件。 您可以通过以下方式实现:
const ParentComponent = () => {
const [isOpen, setIsOpen] = useState(false)
toggleChildMenu() {
setIsOpen(prevValue => !prevValue)
}
return (
<div>
<button onClick={toggleChildMenu}>
Toggle Menu from Parent
</button>
<Child open={isOpen} />
</div>
);
}
const Child = ({open}) => {
return (
<Drawer open={open}/>
);
}
这是我昨天尝试的另一种方法
在您的子脚本中,定义父组件和常规组件可用的方法
var ChildStateModificationFunc;
const Child = ()=>{
const [someState, setSomeState] = useState();
//define the state that you want to modify
ChildStateModificationFunc = (modVal)=>{
setSomeState(modVal)
}
return (
<div>
{/* your child jsx here */}
</div>
}
//export both the child and the method
export default Child;
export {ChildStateModificationFunc}
在您的父脚本中,导入这两个项目
import Child, {ChildStatteModificationFunc} from 'Child.js'
const Parent = ()=>{
var newVal = 'some parent val' //let say you just fetch this from some web api
//share the newVal with Child component
ChildStatteModificationFunc(newVal)
return(
<div>
<Child />
</div>)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.