从父组件更新子状态

时间:2017-05-01 11:37:44

标签: javascript reactjs

让我们说我的父组件有两个子组件:

Parent
| Child1
| Child2

我正在从Child2获得输入,我正在将它传递给Parent组件(直到现在,我知道该怎么做)。但是我需要将该输入传递给Child1以更新它的状态。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

String#join

希望它可以提供帮助

答案 1 :(得分:0)

**组件父**

console.log(text.innerHTML); //That's it ?

Compnent child

   import React from 'react';
    import MM from './modall';
    class App extends React.Component {
        constructor() {
            super();
            this.state = {
                naslov:'',
                telo:''
            };
            this.setStateHandler = this.setStateHandler.bind(this);
            this.postaviStanje = this.postaviStanje.bind(this);
            this.Stanje = this.Stanje.bind(this);
        }
        setStateHandler() {
            this.setState({ naslov: "Naslov Prvi u Modalu", telo:"Novo Prvo telo modala"});

        };
        postaviStanje(){
          this.setState({naslov: " Novi drugi u Modalu", telo:"Novo drugo  telo modala"});
        };
        Stanje(){
          this.setState({naslov: " Novi treci u Modalu", telo:"Novo trece  telo modala"});

        };
        render() {
            return (
                <div>
                    <button onClick = {this.setStateHandler} data-toggle="modal" data-target="#modal">SET STATE</button>
                    <button onClick = {this.postaviStanje} data-toggle="modal" data-target="#modal">SET STATE2</button>
                    <button onClick = {this.Stanje} data-toggle="modal" data-target="#modal">SET STATE3</button>
                    <MM telo={this.state.telo} naslov={this.state.naslov} />)

                </div>
            );
        }
    }
    export default App;