React doesn't require JSX but it's makes writing web apps tremendously easier. It also allows React to show more useful error and warning messages.
React.createElement(component, props, ...children)
is equivalent to <Element prop={prop}><Child /></Element>
class Button extends React.Component {
handleClick() {
console.log('hello world')
}
}
Arguments can be passed into event handlers in two ways:
onClick={event => this.addRow(id, event)}
(passing the event object into the method is optional)onClick={this.addRow.bind(this,id)}
If using the first method above, the order of the arguments is arbitrary. However, if you are using the bind method, id
must be the first parameter in your method definition. Passing your method inside another function allows you to pass in the prop when the event is called. Otherwise writing something like onClick={this.addRow(id)}
will immediately invoke the function and it won't be called when the event actually occurs.
- Bind method in the constructor or when passing props
- Use arrow functions when passing props
onClick={e => this.handleClick(e)}
- Using
createReactClass
(not covered in class)
Note: Defining methods with arrow functions are still being reviewed and should only be used experimentally.