簡體   English   中英

在我的組件中使用React,ES6渲染狀態

[英]Render state with React, ES6 in my component

我正在學習反應,我選擇使用ES6創建一個示例簡歷應用程序。

這是我在App.js代碼App.js

class ContentBlock extends React.Component {
    constructor(props) {
      super(props);
      this.state = { heading: '', body: '' };
    }
    setText() {
      this.setState({ heading: "I'm Daniel", body: "I am about to be twenty seven years old" });
    }
    componentDidMount() {
      {this.setText.bind(this)}
    }
    render() {
      return (
        <Media>
          <Media.Left>
            <Image src={this.props.src}/>
          </Media.Left >
          <Media.Heading>{this.state.heading}</Media.Heading>
          <Media.Body>
            <p>{this.state.body}</p>
          </Media.Body>
        </Media>
      );
    }
  }

我正在使用這三個教程的組合。

官方React ES6教程

BabelJs React ES6教程

關於React的Kirupa教程

除了我的ESLint上的警告,我沒有遇到任何錯誤,但文本沒有渲染。

#####更新

this.props.src來自使用ContentBlock作為子組件的組件

 class About extends Component { render() { return ( <div> <ContentBlock src={danrubio} /> </div> ); } } 

我指定圖像並將其傳遞給ContentBlock將繼承的About組件。

所有這些語句: this.setText.bind(this)確實只是將setText函數綁定到上下文,但不調用它。 更重要的是,您將它綁定到它已有的相同上下文。 這里不需要綁定。

只需改變這個:

componentDidMount() {
  {this.setText.bind(this)}
}

至:

componentDidMount() {
  this.setText()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM