将js设置状态从父组件反应到子组件

时间:2018-01-22 20:22:13

标签: javascript reactjs components parent-child state

所以我有一个父子组件。

父母将搜索栏中键入的内容作为道具传递给孩子们。

然后应该执行api fetch,我在控制台中看到了fetch对象。我在设置子女的状态时遇到了困难。

任何提示将不胜感激,谢谢你和happing编码:D

class HelloComponent extends React.Component {
    render () {
        return <h1>Github API repositories </h1>;
     }
}

class Parent extends React.Component {
    constructor (props) {
        super (props);
        this.boo = this.boo.bind(this);
        this.state = {path: ''};

    }

    boo = (event) => {
        event.preventDefault();
        //alert('it works!');
        let url = 'https://api.github.com/search/repositories?q='+ this.state.path;
        //let parameter = this.state.path;
        console.log(url);
  

我尝试过使用this.props或者只是这个。响应..等等

        axios.get(url)
            .then(response => { 
                console.log(response.data.items)
                this.setState({
                    repo : this.props.response.data.items
                })
            })
    }
    //set the state from the search bar
    searchQuery = (event) => {
        this.setState({ path : event.target.value });
    }


    render() {
        return(
            //call Repositories component and pass the current state as props
            <div>

                <form onSubmit={this.boo}>
                <input type="text" onChange={this.searchQuery}  />
                <input type="submit" value="Search"/>
                </form> 
                <Child search= {this.state.path} />

                {/* <button onClick={this.boo}>fuck</button> */}
            </div>          
        );
    }
}

class Child extends React.Component {
    constructor (props) {
        super (props);
        //this.boo = this.boo.bind(this);
        this.state = { repo : ''};
    }

    render () {
    {/* 
        const titles = this.state.repo.map( (repo) => 
            <tr key={ repo.id }>
            <td> { repo.name }</td>
            <td><a href={repo.html_url}>{repo.html_url}</a></td>
            </tr>
            );


        return (
            <div>
                <table>
                    <tbody>
                        <tr><th>Name</th><th>URL</th></tr>
                        {titles}
                    </tbody>
                </table>    
            </div>
        );
    */}
        return (
            <h1>{this.state.repo}</h1>
        );

    }
}


class App extends React.Component {
    render () {
        return (
        <div>
            <HelloComponent />
            <Parent />
        </div>
        );              
    }
}
ReactDOM.render(<App />, document.getElementById('root'));

1 个答案:

答案 0 :(得分:5)

我认为首先是你的代码的这部分是错误的

axios.get(url)
            .then(response => { 
                console.log(response.data.items)
                this.setState({
                    repo : response.data.items
                })
            })

首先,您需要将道具回购传递给孩子

<Child repo={this.state.repo} search= {this.state.path} />

要从父级设置子级的状态,您不需要在父级中进行任何更改,在其中添加componentWillReceiveProps方法并在其中添加setState

componentWillReceiveProps(nextProps) {
  this.setState({
   repo: nextProps.repo
  })

}