Top Banner
REACT 101 Anatoliy Sieryi Submit PHP 2016
53

React 101 by Anatoliy Sieryi

Jan 06, 2017

Download

Software

Binary Studio
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: React 101 by Anatoliy Sieryi

REACT 101Anatoliy SieryiSubmit PHP 2016

Page 2: React 101 by Anatoliy Sieryi

Anatoliy Sieryi

JS DeveloperBinary Studio Academy

JS group coach

“Sir are you classified as human?”“Negative, I am a meat popsicle”

Page 3: React 101 by Anatoliy Sieryi
Page 4: React 101 by Anatoliy Sieryi
Page 5: React 101 by Anatoliy Sieryi
Page 6: React 101 by Anatoliy Sieryi
Page 7: React 101 by Anatoliy Sieryi
Page 8: React 101 by Anatoliy Sieryi

ReactA JAVASCRIPT LIBRARY FOR BUILDING USER

INTERFACESDeclarativeReact makes it painless to create interactive UIs. Design simple views for each state in your application, and React will efficiently update and render just the right components when your data changes.

Declarative views make your code more predictable and easier to debug.

Component-BasedBuild encapsulated components that manage their own state, then compose them to make complex UIs.

Since component logic is written in JavaScript instead of templates, you can easily pass rich data through your app and keep state out of the DOM.

Learn Once, Write AnywhereWe don't make assumptions about the rest of your technology stack, so you can develop new features in React without rewriting existing code.

React can also render on the server using Node and power mobile apps using React Native.

Page 9: React 101 by Anatoliy Sieryi
Page 10: React 101 by Anatoliy Sieryi

React

◉ Vanilla JS◉ Vanilla JS◉ Vanilla JS◉ Vanilla JS. Kinda◉ Vanilla JS

documentation

Angular 2

◉ Typescript◉ Batteries Included◉ DSL◉ HTML Templates◉ Typescript

documentation

Page 11: React 101 by Anatoliy Sieryi
Page 12: React 101 by Anatoliy Sieryi
Page 13: React 101 by Anatoliy Sieryi
Page 14: React 101 by Anatoliy Sieryi

Hello World Component

// React.createElement(component, [props], [...children])

function Hello(props) {

return React.createElement('div', null, `Hello, ${props.name}`);

}

React.createElement(Hello, { name: "World" });

Page 15: React 101 by Anatoliy Sieryi

Enter The JSX

function Hello(props) {

return <div>{`Hello, ${props.name}`}</div>;

}

<Hello name="World" />

// <div>Hello, World</div>

Page 16: React 101 by Anatoliy Sieryi

JSX example

const element = (

<div>

<h1>Hello!</h1>

<h2>Good to see you here.</h2>

<i className="fa fa-first-order" />

<MyComponent />

</div>

);

Page 17: React 101 by Anatoliy Sieryi

Object - Oriented Style

class Hello extends React.Component {

render() {

return <div>{`Hello, ${this.props.name}`}</div>;

}

}

Page 18: React 101 by Anatoliy Sieryi

Without ES2015

var Hello = React.createClass({

render: function() {

return <h1>Hello, {this.props.name}</h1>;

}

});

Page 19: React 101 by Anatoliy Sieryi

Hello World App / index.html

<html>

<head>

<title>React is Awesome</title>

</head>

<body>

<div id="root"></div>

<script src="app.js"></script>

</body>

</html>

Page 20: React 101 by Anatoliy Sieryi

Hello World App / app.js

import React from 'react';

import ReactDOM from 'react-dom';

import Hello from './components/Hello';

ReactDOM.render(

<Hello name="World" />,

document.getElementById('root')

);

Page 21: React 101 by Anatoliy Sieryi

import React from 'react';

import ListItem from './ListItem';

export class List extends React.Component {

render() {

return (

<div className="list">

{

this.props.list.map(item =>

<ListItem key={list.id} data="list" />)

}

</div>

);

}

}

Page 22: React 101 by Anatoliy Sieryi

import React from 'react';

const warningStyle = {

color: '#ff0000',

fontSize: 'large',

'font-weight': 'bold'

}

export class Warning extends React.Component {

render() {

return (

<div style={warningStyle}>NOOOOOOOO!</div>

);

}

}

Page 23: React 101 by Anatoliy Sieryi

export class SomePage extends React.Component {

render() {

return (

{

this.props.isLoggedIn ?

<Welcome />

:

<Forbidden />

}

);

}

}

Page 24: React 101 by Anatoliy Sieryi

export class MyButton extends React.Component {

handleClick(e) {

alert(e.target.value);

}

render() {

return (

<input

className="my-button"

type="button"

value="Click me!"

onClick={this.handleClick}

/>

);

}

}

Page 25: React 101 by Anatoliy Sieryi

Synthetic Events

boolean bubblesboolean cancelableDOMEventTarget currentTargetboolean defaultPreventednumber eventPhaseboolean isTrustedDOMEvent nativeEvent

void preventDefault()boolean isDefaultPrevented()void stopPropagation()boolean isPropagationStopped()DOMEventTarget targetnumber timeStampstring type

Page 26: React 101 by Anatoliy Sieryi

State

constructor(props) {

super(props);

this.state = {

activeTab: this.props.tabs[0].id,

foo: 'bar'

};

}

onSwitch(id) {

this.setState({ activeTab: id });

}

render() {

const tab = this.props.tabs.find(tab =>

tab.id === this.state.activeTab);

return (

<div className="tabs">

<TabControls

tabs={this.props.tabs}

onSwitch={this.onSwitch.bind(this)}

/>

<TabContent data={tab} />

</div>

);

}

Page 27: React 101 by Anatoliy Sieryi

Refs

<input

type="text"

ref={inputNode => this.textInput = inputNode}

/>

scrollToInput() {

this.textInput.scrollIntoView();

}

<input

type="text"

ref="textInput"

/>

focusOnInput() {

this.refs.textInput.focus();

}

Page 28: React 101 by Anatoliy Sieryi
Page 29: React 101 by Anatoliy Sieryi

Component Lifecycle

Page 30: React 101 by Anatoliy Sieryi

componentDidMount() {

fetch('data.json').then(res => {

this.setState({

data: res.data

});

});

};

componentWillMount() {

this.interval = setInterval(() => {

console.log('tick')

}, 1000);

}

componentWillUnmount() {

clearInterval(this.interval);

}

Page 31: React 101 by Anatoliy Sieryi

React-Router

import { Router, Route, browserHistory } from 'react-router';

// browserHistory / hashHistory / createMemoryHistory

ReactDOM.render((

<Router history={browserHistory}>

<Route path="/" component={App}>

<Route path="about" component={About}/>

<Route path="users" component={Users}>

<Route path="/user/:userId" component={User}/>

</Route>

<Route path="*" component={NoMatch}/>

</Route>

</Router>

), document.getElementById('root'));

Page 32: React 101 by Anatoliy Sieryi
Page 33: React 101 by Anatoliy Sieryi
Page 34: React 101 by Anatoliy Sieryi
Page 35: React 101 by Anatoliy Sieryi
Page 36: React 101 by Anatoliy Sieryi

Basic Configuration

import { createStore, combineReducers } from 'redux';

import { Provider } from 'react-redux';

const rootReducer = combineReducers({ myReducer, myReducer2 });

const store = createStore(rootReducer);

ReactDOM.render(

<Provider store={store}>

<MyRootComponent />

</Provider>,

document.getElementById('root')

);

Page 37: React 101 by Anatoliy Sieryi

Actions

export const DO_A_BARREL_ROLL = 'DO_A_BARREL_ROLL';

export const CHANGE_USER_NAME = 'CHANGE_USER_NAME';

export function doABarrelRoll() {

return {

type: DO_A_BARREL_ROLL

};

}

export function changeUserName(newName) {

return {

type: CHANGE_USER_NAME,

payload: {

newName

}

};

}

Page 38: React 101 by Anatoliy Sieryi

Async Actions (redux-thunk)

export function getCustomers(params) {

return (dispatch, getStore) => {

const store = getStore();

api.getCustomers(params).then(res => {

dispatch(populateCustomers(res.data));

});

};

}

export function populateCustomers(customers) {

return {

type: POPULATE_CUSTOMERS,

payload: {

customers

}

};

}

Page 39: React 101 by Anatoliy Sieryi

Reducer

import { DO_A_BARREL_ROLL } from './MyPageActions';

const initialState = {

barrelRollsDone: 0

};

export default function myPage(state = initialState, action) {

switch (action.type) {

case DO_A_BARREL_ROLL:

return Object.assign({}, state, {

barrelRollsDone: state.barrelRollsDone + 1

});

default:

return state;

}

}

Page 40: React 101 by Anatoliy Sieryi

Container (Smart Component)

import { connect } from 'react-redux';

import { bindActionCreators } from 'redux';

function mapStateToProps(state) {

return {

myPage: state.myPage

};

}

function mapDispatchToProps(dispatch) {

return {

actions: bindActionCreators(Object.assign({}, myPageActions), dispatch)

};

}

export MyComponent;

export default connect(mapStateToProps, mapDispatchToProps)(MyComponent);

Page 41: React 101 by Anatoliy Sieryi
Page 42: React 101 by Anatoliy Sieryi
Page 43: React 101 by Anatoliy Sieryi
Page 44: React 101 by Anatoliy Sieryi
Page 45: React 101 by Anatoliy Sieryi
Page 46: React 101 by Anatoliy Sieryi
Page 47: React 101 by Anatoliy Sieryi
Page 48: React 101 by Anatoliy Sieryi
Page 49: React 101 by Anatoliy Sieryi
Page 50: React 101 by Anatoliy Sieryi
Page 51: React 101 by Anatoliy Sieryi
Page 53: React 101 by Anatoliy Sieryi

THANKS!

Any questions?