1. 父组件向子组件通信

方法: 父组件通过props向子组件传递信息,子组件得到props后可进行相关处理

  1. class Parent extends React.Component {
  2. render() {
  3. return <ChildA title="parent title" />;
  4. }
  5. }
  6. class ChildA extends React.Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. title: this.props.title
  11. };
  12. }
  13. render() {
  14. return <h1>{this.state.title}</h1>;
  15. }
  16. }

2. 子组件向父组件通信

子组件向父组件通信依靠parent传下来的回调函数执行,改变parent组件的状态

case1: 子组件修改父组件的状态state

方法: 父组件向子组件传递一个函数,作为子组件的props,这个函数包含父组件的setState方法,并且接收一个参数,表示父组件的新的state
Parent

  1. class Father extends React.Component {
  2. constructor() {
  3. super();
  4. this.state = { value: "" };
  5. }
  6. setValue = (value) => {
  7. this.setState({
  8. value
  9. });
  10. };
  11. render() {
  12. return (
  13. <div>
  14. <div>I'm Father</div>
  15. <div>value: {this.state.value} </div>
  16. <Child setValue={this.setValue} />
  17. </div>
  18. );
  19. }
  20. }

Child

  1. class Child extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = { inputVal: "" };
  5. }
  6. handleInputChande = (e) => {
  7. this.setState({
  8. inputVal: e.target.value
  9. });
  10. };
  11. handleDivClick = () => {
  12. const { setValue } = this.props;
  13. setValue(this.state.inputVal);
  14. };
  15. render() {
  16. return (
  17. <div>
  18. <div>I'm Child</div>
  19. <input onChange={this.handleInputChande} />
  20. <div onClick={this.handleDivClick}>click to change Father's state</div>
  21. </div>
  22. );
  23. }
  24. }

case2: state定义在子组件中

父组件只是需要拿到子组件的state
方法: 在父组件定义一个函数,接收子组件的state,然后将这个函数以props的形式传递给子组件. 子组件通过this.props拿到这个函数,将子组件本身的state作为函数参数,执行这个函数即可.
Parent

  1. class Parent extends React.Component {
  2. constructor() {
  3. super();
  4. this.state = {
  5. value: ""
  6. };
  7. }
  8. onChange = (value) => {
  9. this.setState({ value });
  10. };
  11. render() {
  12. return (
  13. <div>
  14. <div>I'm Parent</div>
  15. <div>value: {this.state.value}</div>
  16. <Child onChange={this.onChange} />
  17. </div>
  18. );
  19. }
  20. }

Child

  1. class Child extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = {
  5. childValue: ""
  6. };
  7. }
  8. childValChange = (e) => {
  9. this.childVal = e.target.value;
  10. };
  11. childValDispatch = () => {
  12. const { onChange } = this.props;
  13. this.setState(
  14. {
  15. childVal: this.childVal
  16. },
  17. () => {
  18. onChange(this.state.childVal);
  19. }
  20. );
  21. };
  22. render() {
  23. return (
  24. <div>
  25. I'm Child
  26. <div>
  27. state that defined at childVal
  28. <input onChange={this.childValChange} />
  29. <div onClick={this.childValDispatch}>notification</div>
  30. </div>
  31. </div>
  32. );
  33. }
  34. }

3. 兄弟组件间通信

方法1 依赖共同的Container

兄弟组件依赖共有的顶级Container处理或者第三方的状态管理器.
兄弟A的value发生变化,分发的时候把Value值告诉一个中间者C, C会自动告诉B, 实现B的自动Render
Container

  1. class Container extends React.Component {
  2. constructor() {
  3. super();
  4. this.state = {
  5. value: ""
  6. };
  7. }
  8. setValue = (value) => {
  9. this.setState({
  10. value
  11. });
  12. };
  13. render() {
  14. return (
  15. <div>
  16. <div> I'm Container </div>
  17. <ChildA setValue={this.setValue} />
  18. <ChildB value={this.state.value} />
  19. </div>
  20. );
  21. }
  22. }

ChildA

  1. class ChildA extends React.Component {
  2. handleChange = (e) => {
  3. this.value = e.target.value;
  4. };
  5. handleClick = () => {
  6. const setValue = this.props.setValue;
  7. setValue(this.value);
  8. };
  9. render() {
  10. return (
  11. <div>
  12. <div> I'm ChildA</div>
  13. <input onChange={this.handleChange} />
  14. <button onClick={this.handleClick}>notify</button>
  15. </div>
  16. );
  17. }
  18. }

ChildB

  1. function ChildB(props) {
  2. return (
  3. <div>
  4. <div> I'm ChildB</div>
  5. <div> value: {props.value}</div>
  6. </div>
  7. );
  8. }