在ReactJS中从另一个组件更新组件的状态

时间:2018-10-24 14:12:12

标签: javascript reactjs ecmascript-6 syntax jsx

我正在关注本文(原始实现兄弟姐妹兄弟姐妹)Update state cross component

该示例运行良好。但是,当我尝试将每个类分离到每个.js文件时,然后使用导入/导出互相调用/绑定。它(更新状态)不再起作用。 这样的结构:

Sibling1.js

import React, { Component } from 'react';
<-- some declare style -->

export function updateText(text) {
  this.setState({text})
}

export class Sibling1 extends Component {
  render() {
    return (
      <div>
        <div style={{ ...style.topLabel, color: secondaryColor }}>I am Sibling 1</div>
        <input style={style.textBox} type="text"
               placeholder="Write text" onChange={(e) => updateText(e.target.value)} />
      </div>
    )
  }
} 

Example.js

import React, { Component } from 'react';
import * as sibling1 from './Sibling1'; //is this good?
import {Sibling1} from './Sibling1';    //is this good?

<-- some declare style, variable -->

class Sibling2 extends Component {
  constructor(props) {
    super(props)
    this.state = {
      text: "Initial State"
    }
    sibling1.updateText = sibling1.updateText.bind(this)  //is this good binding?
  }
  render() {
    console.log('Sibling2.state : ', this.state);
    return (
      <div>
        <div style={{ ...style.topLabel, color: primaryColor }}>I am Sibling 2</div>
        <div style={style.label}>{this.state.text}</div>
      </div>
    )
  }
}

class Example3 extends Component {
  render() {
    return (
      <div>
        <Sibling1 />
        <Sibling2 />
      </div>
    )
  }
}

export default Example3;

我只是希望Sibling1可以更改Sibling2的状态(就像原始实现一样),但是不能。 我猜我的 bind(this)没有绑定正确的上下文。 有人可以告诉我原始实现(上面的文章)和我的方法(分离到多个.js文件)之间有什么区别吗?

2 个答案:

答案 0 :(得分:2)

updateText()应该绑定到组件。我不确定您要在这里实现什么,但是如果上下文发生变化,updateText()可能在Sibling1中不起作用。

您可以尝试在两个组件中都绑定updateText()(已经在Sibling2中绑定了)。

import React, { Component } from 'react';

export function updateText(text) {
  this.setState({text})
}

export class Sibling1 extends Component {
  constructor() {
    updateText = updateText.bind(this)
  }
  render() {
    return (
      <div>
        <div style={{ ...style.topLabel, color: secondaryColor }}>I am Sibling 1</div>
        <input style={style.textBox} type="text"
               placeholder="Write text" onChange={(e) => updateText(e.target.value)} />
      </div>
    )
  }
}

通常,如果两个子组件需要共享状态并且仅将处理程序传递给儿童,则在父组件中控制状态。

答案 1 :(得分:1)

React kinda强制您使用单向数据流。因此,您不能只是从Sibling2中更新Sibling1的状态。

就像Dinesh Pandiyan在他的示例中提到的那样,通常您将拥有一个控制两个兄弟姐妹状态的父组件。您的代码将如下所示:

Sibling1.js

import React, { Component } from 'react';
<-- some declare style -->

export class Sibling1 extends Component {
  function updateText(text) {
    // Use updateText function from props.
    // Props are like state but not controlled by the component itself
    // The value is passed to the component from outside
    this.props.updateText(text)
  }

  render() {
    return (
      <div>
        <div style={{ ...style.topLabel, color: secondaryColor }}>I am Sibling 1</div>
        <input style={style.textBox} type="text"
               placeholder="Write text" 
               onChange={(e) => this.updateText(e.target.value).bind(this)} />
      </div>
    )
  }
} 

Example.js

import React, { Component } from 'react';
import { Sibling1 } from './Sibling1';    // This is good.
import Sibling1 from './Sibling1'; // This is also possible if you use export default class instead of export class

<-- some declare style, variable -->

class Sibling2 extends Component {
  // Use same function as in Sibling1.
  function updateText(text) {
      this.props.updateText(text)
  }

  render() {
    return (
      <div>
        <div style={{ ...style.topLabel, color: primaryColor }}>I am Sibling 2</div>
        <div style={style.label}>{this.props.text}</div> // changed state to props
      </div>
    )
  }
}

class Example3 extends Component {
  constructor(props) {
    super(props);


    this.state = {
      text: "Initial state"
    };
  }

  // Control state from parent component
  function updateText(
    this.setState({ text: text });
  }

  render() {
    return (
      <div>
        <Sibling1 updateText={this.updateText.bind(this)}/>
        <Sibling2 updateText={this.updateText.bind(this)} text={this.state.text} />
      </div>
    )
  }
}

export default Example3;