javascript - How to use arrow functions inside React.createClass - Stack Overflow

I want to use ES6 (ES2015) as much as possible for my small project. Now I want to use arrow functions

I want to use ES6 (ES2015) as much as possible for my small project. Now I want to use arrow functions with React.

// What I want
let Text = React.createClass({
    ponentDidMount: () => {
        setInterval(this.updateCurrentTime, 1000);
    }
}

// What I have
let Paragraph = React.createClass({
    render: () => (<div className="MySuperTable"><Text name="Dodo" startDate={(new Date()).toString()} /></div>)
});

let Text = React.createClass({
    getInitialState: function () {
        return {
            currentTime: (new Date()).toString()
        }
    },
    ponentDidMount: function () {
        setInterval(this.updateCurrentTime, 1000);
    },
    updateCurrentTime: function () {
        this.setState({
            currentTime: (new Date()).toString()
        });
    },
    render: function () {
        return (
            <div>
                <span>Hello my name is {this.props.name}</span>
                <span>And I was born on {this.props.startDate}</span>
                <span>And I now it's {this.state.currentTime}</span>
            </div>
        );
    }
});

ReactDOM.render(
    <Paragraph/>,
    document.getElementById('container')
);
  1. What do I have to do to get this working?
  2. As I understand this will be the object passed into createClass itself, is this correct?
  3. How do I bind it to the Text instance?

Thanks in advance.

I want to use ES6 (ES2015) as much as possible for my small project. Now I want to use arrow functions with React.

// What I want
let Text = React.createClass({
    ponentDidMount: () => {
        setInterval(this.updateCurrentTime, 1000);
    }
}

// What I have
let Paragraph = React.createClass({
    render: () => (<div className="MySuperTable"><Text name="Dodo" startDate={(new Date()).toString()} /></div>)
});

let Text = React.createClass({
    getInitialState: function () {
        return {
            currentTime: (new Date()).toString()
        }
    },
    ponentDidMount: function () {
        setInterval(this.updateCurrentTime, 1000);
    },
    updateCurrentTime: function () {
        this.setState({
            currentTime: (new Date()).toString()
        });
    },
    render: function () {
        return (
            <div>
                <span>Hello my name is {this.props.name}</span>
                <span>And I was born on {this.props.startDate}</span>
                <span>And I now it's {this.state.currentTime}</span>
            </div>
        );
    }
});

ReactDOM.render(
    <Paragraph/>,
    document.getElementById('container')
);
  1. What do I have to do to get this working?
  2. As I understand this will be the object passed into createClass itself, is this correct?
  3. How do I bind it to the Text instance?

Thanks in advance.

Share Improve this question edited May 25, 2017 at 20:48 icc97 12.9k9 gold badges83 silver badges97 bronze badges asked Mar 18, 2016 at 9:13 RantievRantiev 2,2633 gold badges38 silver badges60 bronze badges 2
  • Possible duplicate of Arrow function vs function declaration / expressions: Are they equivalent / exchangeable? – Felix Kling Commented Mar 18, 2016 at 13:28
  • You cannot simply replace normal functions with arrow functions. – Felix Kling Commented Mar 18, 2016 at 13:29
Add a ment  | 

2 Answers 2

Reset to default 5

You can change your code with ES2015 like this

class Text extends React.Component {
  constructor() {
    super();
    this.state = { currentTime: (new Date()).toString() };
  }

  ponentDidMount() {
    setInterval(() => this.updateCurrentTime(), 1000);
  }

  updateCurrentTime() {
    this.setState({
      currentTime: (new Date()).toString()
    });
  }

  render() {
    return <div>
      <span>Hello my name is { this.props.name }</span>
      <span>And i was born { this.props.startDate }</span>
      <span>And i now it's { this.state.currentTime }</span>
    </div>  
  }
};

let Paragraph = () => {
  return <div className="MySuperTable">
    <Text name="Dodo" startDate={ (new Date()).toString() } />
  </div>
};

Example

React - Reusable Components

As you said, you wanted use ES6 as much as possible. So then instead of using createClass, you can use 'React.Component', you can find more info here.

And then to use, arrow mark you can use Babel, preset-2015.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744389110a4571816.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信