react js set state from parent to child component

社会主义新天地 提交于 2021-02-08 03:28:26

问题


So I have a parent and children component.

Parent passes whatever is typed in the search bar as a prop to the children.

then the api fetch should be executed, I see the fetch object in the console. I'm having difficulties setting the children state from the parent.

Any tips would be appreciated, thank you and happing coding :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);

I tried using this.props or just this.response..etc

        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:


I think first is this part of your code is wrong

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

First you need to pass prop repo to child

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

To set state of child from parent, you don't need to make any change in parent, Add componentWillReceiveProps method in and setState there

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

}


来源:https://stackoverflow.com/questions/48389653/react-js-set-state-from-parent-to-child-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!