Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 子组件的传递状态_Reactjs_Ecmascript 6_React Props - Fatal编程技术网

Reactjs 子组件的传递状态

Reactjs 子组件的传递状态,reactjs,ecmascript-6,react-props,Reactjs,Ecmascript 6,React Props,所以我有一个组件“itemSelection”,在它里面我通过这样的api响应进行映射 <div className="row"> {this.state.items.map(i => <Item name={i.name} quantity={i.quantity} />)} </div> 如何将“Item”组件的状态传递给“itemSelection”组件?应该使用道具将数据发送回父组件。 相当常见的问题,请参阅以获取详细答案 根据我的

所以我有一个组件“itemSelection”,在它里面我通过这样的api响应进行映射

<div className="row">
     {this.state.items.map(i => <Item name={i.name} quantity={i.quantity} />)}
</div> 

如何将“Item”组件的状态传递给“itemSelection”组件?

应该使用道具将数据发送回父组件。
相当常见的问题,请参阅以获取详细答案

根据我的说法,如果我理解了你的问题,你想将子组件的状态调用到父组件

//Child.js

import s from './Child.css';

class Child extends Component {
 getAlert() {
    alert('clicked');
 }
 render() {
  return (
    <h1 ref="hello">Hello</h1>
  );
 }
}

export default withStyles(s)(Child);

//Parent.js

class Parent extends Component {
 render() {

  onClick() {
    this.refs.child.getAlert()
  }

  return (
    <div>
      <Child ref="child" />
      <button onClick={this.onClick.bind(this)}>Click</button>
    </div>
  );
 }
}
//Child.js
从“/Child.css”导入s;
类子扩展组件{
getAlert(){
警报(“点击”);
}
render(){
返回(
你好
);
}
}
导出默认样式(子项);
//Parent.js
类父级扩展组件{
render(){
onClick(){
this.refs.child.getAlert()
}
返回(
点击
);
}
}
此外,您还可以从以下链接获取代码参考:

这有点棘手,但可能会帮助你解决问题

//Parent.js
class Parent extends Component {
    component(props) {
        super(props)

        this.state = {
            test: 'abc'
        }
    }

    ParentFunction = (value) => {
        this.state.test = value;
        this.setState(this.state);
    }

    render() {

        return (
            <div>
                <Child
                    test={this.state.test}
                    ParentFunction={this.ParentFunction}
                />
            </div>
        );
    }
}


//Child.js

import s from './Child.css';

class Child extends Component {
    component(props) {
        super(props)

        this.state = {
            test: props.test
        }
    }

    handleChange = () => {
        this.state.test = event.target.value;
        this.setState(this.state);
        this.handleOnSave()
    }

    handleOnSave = () => {
        this.props.ParentFunction(this.state.test);
    }

    render() {
        return (
            <div> 
                <input type="text" onChange={this.handleChange} />
            </div>
        );
    }
}

export default withStyles(s)(Child);
//Parent.js
类父级扩展组件{
组件(道具){
超级(道具)
此.state={
测试:“abc”
}
}
父函数=(值)=>{
this.state.test=值;
this.setState(this.state);
}
render(){
返回(
);
}
}
//Child.js
从“/Child.css”导入s;
类子扩展组件{
组件(道具){
超级(道具)
此.state={
测试:道具测试
}
}
handleChange=()=>{
this.state.test=event.target.value;
this.setState(this.state);
这个。handleOnSave()
}
handleOnSave=()=>{
this.props.ParentFunction(this.state.test);
}
render(){
返回(
);
}
}
导出默认样式(子项);

您很可能需要将回调从
ItemSelection
传递到
Item
。你能粘贴这两个组件的完整代码吗?感谢使用道具将状态传递给itemSelection组件。为什么您一次又一次地发布相同的问题。你前面的问题已经有人回答了。即使我在一小时前回答了,我也需要将孩子的状态传递给父母,而不是孩子opposite@Gazzar2这正是我在这里描述的?你必须以国家为依托,向上传递ut。本视频还介绍了解决方案:非常感谢,但我想在子状态中使用refs的是什么?假设子状态中有一个名为“selected:false”的属性。如果我想将其传递给父对象中的属性,该怎么办?我应该这样做“parentSelected:this.refs.child.state”吗?
//Parent.js
class Parent extends Component {
    component(props) {
        super(props)

        this.state = {
            test: 'abc'
        }
    }

    ParentFunction = (value) => {
        this.state.test = value;
        this.setState(this.state);
    }

    render() {

        return (
            <div>
                <Child
                    test={this.state.test}
                    ParentFunction={this.ParentFunction}
                />
            </div>
        );
    }
}


//Child.js

import s from './Child.css';

class Child extends Component {
    component(props) {
        super(props)

        this.state = {
            test: props.test
        }
    }

    handleChange = () => {
        this.state.test = event.target.value;
        this.setState(this.state);
        this.handleOnSave()
    }

    handleOnSave = () => {
        this.props.ParentFunction(this.state.test);
    }

    render() {
        return (
            <div> 
                <input type="text" onChange={this.handleChange} />
            </div>
        );
    }
}

export default withStyles(s)(Child);