the Bad
Most important terms in React
All the API are available on the React.*.
Component
PureComponent
createElement(type, [props], [...children])
cloneElement(element, [props], [...children])
createFactory(type)
isValidElement(object)
Children // map, forEach, count, only, toArray
Fragment
render(){
return(
<div>Hello</div>// JSX
)
}
Components are self-contained reusable building blocks of web application.
React components are basically just idempotent functions (same input produces same output).
They describe your UI at any point in time, just like a server-rendered app.
import React, { Component } from 'react';
Class App extends Component {
Render(){
returns(
<div>Hello</div>
)
}
}
export default App
import React, { Component } from 'react';
Class App extends Component {
Render(){
returns(
<div>Hello</div>
)
}
}
export default App
import React, { Component } from 'react';
Const App = () => <div>Hello</div>
export default App
OR
import React, { Component } from 'react';
Const App = () =>
render() {
return (
<div>Hello</div>
)
}
export default App
Smart components are stateful. They may have a state and lifecycles. So, the component will re-render every time you set the new state or receiving new props.
...
var News = React.createClass({
render: function() {
var data = this.props.data;
var newsTemplate = data.map(function(item, index) {
return (
<div key={index}>
<p className="news__author">{item.author}:</p>
<p className="news__text">{item.text}</p>
</div>
)
})
console.log(newsTemplate);
return (
<div className="news">
{newsTemplate}
</div>
);
}
});
...
import React from 'react';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
header: "Header from state...",
content: "Content from state..."
}
}
render() {
return (
<div>
<h1>{this.state.header}</h1>
<h2>{this.state.content}</h2>
</div>
);
}
}
export default App;
function ListComponent(props) {
const listItems = myList.map((item) =>
<li key={item.id}>
{item.value}
</li>
);
return (
<ul>{listItems}</ul>
);
}
const myList = [{id: 'a', value: 'apple'},
{id: 'b', value: 'orange'},
{id: 'c', value: 'strawberry'},
{id: 'd', value: 'blueberry'},
{id: 'e', value: 'avocado'}];
ReactDOM.render(
<ListComponent myList={myList} />,
document.getElementById('root')
);
import React from 'react';
import axios from 'axios';
export default class PersonList extends React.Component {
state = {
persons: []
}
componentDidMount() {
axios.get(`https://jsonplaceholder.typicode.com/users`)
.then(res => {
const persons = res.data;
this.setState({ persons });
})
}
render() {
return (
<ul>
{ this.state.persons.map(person => <li>{person.name}</li>)}
</ul>
)
}
}
const myPost = {
title: 'A post about true facts',
body: '42',
userId: 2
}
const options = {
method: 'POST',
body: JSON.stringify(myPost),
headers: {
'Content-Type': 'application/json'
}
};
fetch('https://jsonplaceholder.typicode.com/posts', options)
.then(res => res.json())
.then(res => console.log(res));
Conception