You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
2.0 KiB
60 lines
2.0 KiB
8 years ago
|
---
|
||
|
id: react-without-jsx
|
||
|
title: React Without JSX
|
||
|
permalink: docs/react-without-jsx.html
|
||
|
---
|
||
|
|
||
|
JSX is not a requirement for using React. Using React without JSX is especially convenient when you don't want to set up compilation in your build environment.
|
||
|
|
||
|
Each JSX element is just syntactic sugar for calling `React.createElement(component, props, ...children)`. So, anything you can do with JSX can also be done with just plain JavaScript.
|
||
|
|
||
|
For example, this code written with JSX:
|
||
|
|
||
|
```js
|
||
|
class Hello extends React.Component {
|
||
|
render() {
|
||
|
return <div>Hello {this.props.toWhat}</div>;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ReactDOM.render(
|
||
|
<Hello toWhat="World" />,
|
||
|
document.getElementById('root')
|
||
|
);
|
||
|
```
|
||
|
|
||
|
can be compiled to this code that does not use JSX:
|
||
|
|
||
|
```js
|
||
|
class Hello extends React.Component {
|
||
|
render() {
|
||
|
return React.createElement('div', null, `Hello ${this.props.toWhat}`);
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
ReactDOM.render(
|
||
|
React.createElement(Hello, {toWhat: 'World'}, null),
|
||
|
document.getElementById('root')
|
||
|
);
|
||
8 years ago
|
```
|
||
|
|
||
|
If you're curious to see more examples of how JSX is converted to JavaScript, you can try out [the online Babel compiler](https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015%2Creact%2Cstage-0&code=function%20hello()%20%7B%0A%20%20return%20%3Cdiv%3EHello%20world!%3C%2Fdiv%3E%3B%0A%7D).
|
||
|
|
||
|
The component can either be provided as a string, or as a subclass of `React.Component`, or a plain function for stateless components.
|
||
|
|
||
|
If you get tired of typing `React.createElement` so much, one common pattern is to assign a shorthand:
|
||
|
|
||
|
```js
|
||
8 years ago
|
const e = React.createElement;
|
||
8 years ago
|
|
||
8 years ago
|
ReactDOM.render(
|
||
|
e('div', null, 'Hello World'),
|
||
|
document.getElementById('root')
|
||
|
);
|
||
8 years ago
|
```
|
||
|
|
||
|
If you use this shorthand form for `React.createElement`, it can be almost as convenient to use React without JSX.
|
||
8 years ago
|
|
||
|
Alternatively, you can refer to community projects such as [`react-hyperscript`](https://github.com/mlmorg/react-hyperscript) and [`hyperscript-helpers`](https://github.com/ohanhi/hyperscript-helpers) which offer a terser syntax.
|
||
|
|