React - Render UI with React
10/8/2017
Intro
- React elements: JavaScript objects
- React elements -> DOM nodes
- React components: custom elements
Creating Elements and JSX
React.createElement
React.createElement(type, props, content);- Creates a single React element of particular type.
type- string (
<div>,<span>) - React component
- string (
propsnull- An object of HTML attributes and custom data about the element
contentnull- String
- React element
- React component
Example - basic
import React from 'react';
import ReactDOM from 'react-dom';
const element = React.createElement(
'div',
{ className: 'welcome-message' },
React.createElement('strong', null, 'hello world')
);
ReactDOM.render(element, document.getElementById('root'));- Apps built with React have a single
rootDOM node. elementis inserted into therootDOM node.
Example - dynamic list
import React from 'react';
import ReactDOM from 'react-dom';
const people = [{ name: 'Michael' }, { name: 'Ryan' }, { name: 'Tyler' }];
const element = React.createElement(
'ol',
null,
people.map((person, index) =>
React.createElement('li', { key: person.name, id: index }, person.name)
)
);
ReactDOM.render(element, document.getElementById('root'));- A unique
keyprop is needed when defining a list.keyhelps React track specific child elements as the state changes in the app.
JSX
- A syntax extention to
React.createElement. - JSX is compiled to JavaScript.
- JSX returns a single element as well.
Example
import React from 'react';
import ReactDOM from 'react-dom';
const people = [{ name: 'Michael' }, { name: 'Ryan' }, { name: 'Tyler' }];
const element = (
<ol>
{people.map((person, index) => (
<li id={index} key={person.name}>
{person.name}
</li>
))}
</ol>
);
ReactDOM.render(element, document.getElementById('root'));React component
- Reusable pieces of code that groups React elements together.
- Factory to create React elements.
- Component should have
- Clear responsibilities.
- Well-defined interfaces.
- Required method:
render.
Example
import React from 'react';
import ReactDOM from 'react-dom';
class ContactList extends React.Component {
render() {
const people = [{ name: 'Michael' }, { name: 'Ryan' }, { name: 'Tyler' }];
const element = (
<ol>
{people.map((person, index) => (
<li id={index} key={person.name}>
{person.name}
</li>
))}
</ol>
);
}
}
ReactDOM.render(<ContactList />, document.getElementById('root'));
// Use the following import statement to simplify component declaration:
// import React, { Component } from 'react'
// class ContactList extends ComponentCreate React App
Webpackbundles all assets (JavaScript files, CSS, images, etc).Babeltranspiles JSX to JavaScript.create-react-appmanages all the above setup.
npm install -g create-react-app
## -g: install globally
create-react-app <app>
## installs react, react-dom and react-scripts
## creates the app
cd <app>/
yarn start
## package manager by facebook- Edit
App.jsfile. index.jsreads inApp.jsand renders it.
Composing with Components
- Component composition
- Reuse components
- Allow different configuration of components
class ContactList extends Component {
render() {
const people = this.props.contacts;
// ...
}
}
class App extends Component {
render() {
return (
<div className="App">
<ContactList contacts={[{ name: 'Michael' }, { name: 'Ryan' }, { name: 'Tyler' }]} />
<ContactList contacts={[{ name: 'Amanda' }, { name: 'Richard' }, { name: 'Geoff' }]} />
</div>
);
}
}Outro
- JSX uses JavaScript to describe UI elements.
- React components group UI elements, and can be composed together.
create-react-app