ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-11-08 12:33:08  阅读:439  来源: 互联网

标签:jsx ecmascript-6 reactjs syntax javascript


我正在关注本文(原始实现Sibling Sibling):
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文件)之间的区别是什么?

解决方法:

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;

标签:jsx,ecmascript-6,reactjs,syntax,javascript
来源: https://codeday.me/bug/20191108/2008191.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有