ReactJS:将数据从子级传递到父级

时间:2019-01-17 08:17:47

标签: javascript reactjs

我刚拿起React并不能解决这个问题。

我有一个嵌入在主应用程序组件中的搜索组件。 在搜索组件中,我使用搜索栏中的输入值进行API调用。但是我不知道如何将数据返回给父级(我目前在这里使用类似于API将返回的数据的硬编码状态对象),以将I传递给另一个组件。

基本上:从子级中的API获取数据,将其传递给父级,然后再传递给另外2个子级

import React, {Component} from 'react';
import GroupItem from './components/GroupItem'
import SearchBar from './components/SearchBar'

class App extends Component {

  state = {
    views: [
        {
            name: 'Testname',
            description: 'testbeschreibung',
            status: 'ok'
        },
        {
            name: 'Ein Anderer Titel',
            description: 'lorem ipsum',
            status: 'ok'
        }
    ],
    commands: [
        {
            name: 'Wieder etwas',
            description: 'testbeschreibung',
            status: 'ok'
        }
    ],
    search : []
}


render()
{
    return (
        <div>
            <SearchBar/>
            <h2>Views </h2>
            {this.state.views.map((view) => (<GroupItem data={view} type={'view'} />))}
            <h2>Commands</h2>
            {this.state.commands.map((command) => (<GroupItem data={command} type={'command'} />))}
        </div>
    );
}
}

export default App;


import React, {Component} from 'react';

class SearchBar extends Component {

    callApi(){
        let search = this.refs.search.value;
        fetch('http://my-awesome.api/'+search)
            .then((result) => {
                return result.json();
            }).then((jsonResult) => {
            console.log(jsonResult);
        })
    }

    render() {
        return (
            <div class="input-group mb-3" id="search">
                <input type="text" class="form-control" ref="search" placeholder="URL" aria-label="URL" />
                    <div class="input-group-append">
                        <button class="btn btn-outline-secondary" type="button" onClick={() => this.callApi()}>Search</button>
                    </div>
            </div>
        );
    }
}

export default SearchBar;

1 个答案:

答案 0 :(得分:1)

您需要从具有 prop App Component 中传递处理程序,并更新状态,请检查下面的代码编辑过。

import React, {Component} from 'react';
import GroupItem from './components/GroupItem'
import SearchBar from './components/SearchBar'

class App extends Component {

  state = {
    views: [
        {
            name: 'Testname',
            description: 'testbeschreibung',
            status: 'ok'
        },
        {
            name: 'Ein Anderer Titel',
            description: 'lorem ipsum',
            status: 'ok'
        }
    ],
    commands: [
        {
            name: 'Wieder etwas',
            description: 'testbeschreibung',
            status: 'ok'
        }
    ],
    search : []
}

handleSearchFill = (data) =>{
  this.setState({search:data})
}


render()
{
    return (
        <div>
            <SearchBar searchFill={this.handleSearchFill}/>
            <h2>Views </h2>
            {this.state.views.map((view) => (<GroupItem data={view} type={'view'} />))}
            <h2>Commands</h2>
            {this.state.commands.map((command) => (<GroupItem data={command} type={'command'} />))}
        </div>
    );
}
}

export default App;


import React, {Component} from 'react';

class SearchBar extends Component {

    callApi(){
        let search = this.refs.search.value;
        fetch('http://my-awesome.api/'+search)
            .then((result) => {
                this.props.searchFill(result.json());
            }).then((jsonResult) => {
            console.log(jsonResult);
        })
    }

    render() {
        return (
            <div class="input-group mb-3" id="search">
                <input type="text" class="form-control" ref="search" placeholder="URL" aria-label="URL" />
                    <div class="input-group-append">
                        <button class="btn btn-outline-secondary" type="button" onClick={() => this.callApi()}>Search</button>
                    </div>
            </div>
        );
    }
}

export default SearchBar;