Reactjs Sibling Component未获取更新的状态值

时间:2017-03-08 00:38:51

标签: reactjs react-jsx

1)如何将此值作为prop1传递给Child1组件 2)如果我只是将状态值传递给Child1组件,我没有得到Child1中的更新值 -

关注不同的文章 - 关于在兄弟姐妹之间传递道具但没有任何作用.. 父:

export default class Parent extends Component {

   constructor(props) {
    super(props);
    this.state = {
      toggledata: '',
    };
  }

   handleToggle(value) { //getting the updated value to 'value'
    this.setState({ toggledata: value });
  }

  render() {
    return (
      <div>
        <Child1 ToggleStatus={this.state.toggledata} />
        <Child2 callbackFromParent={this.handleToggle.bind(this)} />
      </div>
    );
  }
}
Parent.propTypes = {
  params: PropTypes.object,
};

CHILD2:

class Child1 extends Component {
constructor(props) {
    super(props);
    this.state = {
      text: '',
    };
  }

  handleClick(event) {
    this.setState({ text: 'green' }, () => {
      this.props.callbackFromParent(this.state.text);
    });
  }

render(){
return (
          <a onClick={() => { this.handleClick(event) }} href="">
            Click me
          </a>

    );
  }
}
export default Child1;

1 个答案:

答案 0 :(得分:0)

您没有将 body {margin:0; background-image: url("Road.png"); background-repeat: no-repeat; background-position: center; background-size: cover; } p { font-family: Verdana; font-size: x-large; text-align: center; margin-top: 300px; } h1 { text-align: center; font-family: Verdana; font-size: x-large; font-style: italic; } a { text-decoration: none; color: white; } .Coming Soon { color: white; } .Logo { width: 130px; height: 80px; margin-right: 100px; } .socialmedia { position:fixed; right:150px; top:35px; transform:translate(0,-50%); display: flex; /* add this */ align-items: center; /* add this */ } .preorder button { background-color: white; border: 0; height: 35px; width: 110px; margin-left: 35px; } .footer { display: flex; align-items: center; width: 100%; height: 90px; margin-top: 319px; }绑定到回调。 你的父母没有调用<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> <title>WEBSITE TITLE HERE</title> <!-- Bootstrap Core CSS --> <link href="css/bootstrap.min.css" rel="stylesheet"> <!-- Custom CSS: You can use this stylesheet to override any Bootstrap styles and/or apply your own styles --> <link href="css/custom.css" rel="stylesheet"> <script src="https://use.fontawesome.com/a0aac8df13.js"></script> <meta name="Description" content="ADD DESCRIPTION."> <meta name="Keywords" content="ADD KEYWORDS"> <link rel="icon" href="Logo.png" type="img/SVG" style="width: 100%; height: 100%"> </head> <body> <div class="container"> <div class="Coming Soon"> <h1>Some Header</h1> <div class="socialmedia"> <img src="Logo.png" style=" width: 130px; height: 80px; margin-right: 100px"> <a class="Facebook"> <a href="https://www.facebook.com/" target="_blank"><img src="https://images.seeklogo.net/2016/09/facebook-icon-preview-1.png" width="50px" height="50px"></a> </a> <a class="Instagram"> <a href="https://www.instagram.com/" target="_blank"><img src="https://images.seeklogo.net/2016/06/Instagram-logo.png" width="50px" height="50px"></a> </a> <a class="Youtube"> <a href="https://www.youtube.com/channel" target="_blank"><img src="https://images.seeklogo.net/2016/06/YouTube-icon.png" width="50px" height="50px"></a> </a> </div> <p>Some Paragraph</p> <div class="footer"> <p style="font-family: Verdana; font-size: small; padding-left: 55%;">2017 Some Company LLC | City State Company Website All Right Reserved.</p> </div></div></div> </body> </html>,而是在this内调用它。 this.setStateChild2的上下文来自Child2,如果不绑定它,则不是Parent。

this

相关问题