当前位置 : 主页 > 网络编程 > JavaScript >

State两种绑定方式

来源:互联网 收集:自由互联 发布时间:2021-06-28
片段1 import React from 'react';class App extends React.Component { constructor() { super(); this.state = { data: [] }; this.setStateHandler = this.setStateHandler.bind(this); }; setStateHandler() { var item = "设置状态..."; var myArr
片段1
import React from 'react';

class App extends React.Component {
  constructor() {
    super();

    this.state = {
      data: []
    };

    this.setStateHandler = this.setStateHandler.bind(this);
  };

  setStateHandler() {
    var item = "设置状态...";
    var myArray = this.state.data;
    myArray.push(item);
    this.setState({data: myArray});
  };

  render() {
    return (
      
 
        
  
        
  

State Array: {this.state.data.join()}

); } } export default App;
片段2
import React from 'react';

class App extends React.Component {
  constructor() {
    super();

    this.state = {
      data: []
    };
  };

  setStateHandler() {
    var item = "设置状态...";
    var myArray = this.state.data;
    myArray.push(item);
    this.setState({data: myArray});
  };

  render() {
    return (
      
 
        
  
        
  

State Array: {this.state.data.join()}

); } } export default App;
网友评论