ES6 syntax equivalent:
import React, { Component, PropTypes } from 'react' class Todo extends Component { render() { const { onClick, completed, text } = this.props return ( <li onClick={onClick} style={{ textDecoration: completed ? 'line-through' : 'none' }} > {text} </li> ) } } Todo.propTypes = { onClick: PropTypes.func.isRequired, completed: PropTypes.bool.isRequired, text: PropTypes.string.isRequired } export default Todo
If you use babel to convert your code, and you have class property properties , you can do the following:
import React, { Component, PropTypes } from 'react' class Todo extends Component { static propTypes = { onClick: PropTypes.func.isRequired, completed: PropTypes.bool.isRequired, text: PropTypes.string.isRequired } render() { const { onClick, completed, text } = this.props return ( <li onClick={onClick} style={{ textDecoration: completed ? 'line-through' : 'none' }} > {text} </li> ) } } export default Todo
Just to be clear, this second example cannot be considered “standard ES6,” but I think the static properties are so clean that I thought it was worth showing this approach as well.
source share