Reactjs 在fetch-then块中将React道具从子级传递到父级

Reactjs 在fetch-then块中将React道具从子级传递到父级,reactjs,react-native,redux,react-redux,Reactjs,React Native,Redux,React Redux,我想把道具从孩子传给父母。我从子类中的API请求中获取数据,但当我使用回调将其传递给父类时,它会向我发送空数组,尽管在子类中检查它时它已满。我找不到问题出在哪里。有什么建议吗? 注意:我在fetch()中使用回调。然后阻塞 这是父类 class Parent extends React.Component { constructor(props) { super(props); this.state = { listDataFro

我想把道具从孩子传给父母。我从子类中的API请求中获取数据,但当我使用回调将其传递给父类时,它会向我发送空数组,尽管在子类中检查它时它已满。我找不到问题出在哪里。有什么建议吗? 注意:我在fetch()中使用回调。然后阻塞

这是父类

class Parent extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            listDataFromChild: null
        };
        this.getData = this.getData.bind(this);
    }

    getData(val){
        this.setState({listDataFromChild: val});
        console.log(val.length);
    }

    render() {
        return (

            <div className="row">
                <div className="col-md-2">
                    <TableSearchForm callbackFromParent = {this.getData}/>
                </div>
                <div className="col-md-8">
                    <Table data = {this.state.listDataFromChild}/>
                </div>
            </div>
        )
    }
这里

.then(this.props.callbackFromParent(result))

then
函数,但您在
then
内部调用该函数,这意味着您正在将该函数的返回值传递给
then
。因此,向
传递一个函数,然后像这样

  .then(function(response){
    return response.json() 
  }).then( function (json) {
        result = json[0]["col1"];
        return result;
  }).then((result) => {
        this.props.callbackFromParent(result)
  });

这样做更好

 constructor(props) {
        super(props);
        this.props = props;

        this.state = {
            data: [] , 
            value:[]
         };
        this.makeRequest = this.makeRequest.bind(this);
    }
而不是这个

 constructor(props) {
        super(props);
        this.props = props;
        this.state = { value: '' };
        this.state = {data: []};
        this.makeRequest = this.makeRequest.bind(this);
    }

你理解
然后(a)
然后(a())
之间的区别吗?这还不够,上下文丢失了。
 constructor(props) {
        super(props);
        this.props = props;
        this.state = { value: '' };
        this.state = {data: []};
        this.makeRequest = this.makeRequest.bind(this);
    }