React.js EcoSystem

Breathe! It’s web development, Things move fast

You don’t need to master everything and there will always be that brand new technology that the cool kids will gonna want to use. [*]

Let's look at some useful libraries and packages

in React EcoSystem

Create-React-App

It also supports flexible application profiling using environment variables, backend proxies for local development, Flow, and other features.

Create-React-App

React-Router

const App = () => (
    <BrowserRouter>
        {/* here's a div */}
        <div>
            {/* here's a Route */}
            <Route path="/tacos" component={Tacos}/>
        </div>
    </BrowserRouter>
)

Components are the heart of React's powerful, declarative programming model. Whether you want to have bookmarkable URLs for your web app or a composable way to navigate in React Native, React Router works wherever React is rendering--so take your pick!

Axios

axios.get('/user',
    {params:
        { ID: 12345 }
    }
).then(
    function (response) {
        console.log(response);
    }
).catch(
    function (error) {
        console.log(error);
    }
);

Material-UI

import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import RaisedButton from 'material-ui/RaisedButton';
import injectTapEventPlugin from 'react-tap-event-plugin';

injectTapEventPlugin();

const App = () => (
    <MuiThemeProvider>
        <RaisedButton label="Default" />
    </MuiThemeProvider>
);

React-Notification-System

Expect

  • expect lets you write better assertions.
expect([ 1, 2, 3 ]).toInclude(3)
expect({ a: 1, b: 2 }).toInclude({ b: 2 })
expect({ a: 1, b: 2, c: { d: 3 } })
    .toInclude({ b: 2, c: { d: 3 } })
expect('hello world').toInclude('world')

Jest

function sum(a, b) {
    return a + b;
}

test('adds 1 + 2 to equal 3', () => {
    expect(sum(1, 2)).toBe(3);
});

React-Native

import React, { Component } from 'react';
import { Text, View } from 'react-native';

class WhyReactNativeIsSoGreat extends Component {
    render() {
        return (
            <View>
                <Text>
                    You just use native components like 'View' and 'Text',
                    instead of web components like 'div' and 'span'.
                </Text>
            </View>
        );
    }
}

And more important libraries

Redux

It was inspired by Flux but with some simplifications.

If you’re working on a simple project, then introducing Redux might be an over complication, but for medium and large-scale projects, it’s a solid choice. The library has become so popular that there are projects implementing it in Angular as well.

Redux Principles

  1. The whole state of the application is represented in a single JS Object
  2. State is read-only. Changes are done through dispatching actions, only requirement for an action is it needs to have a "type" !
  3. React: View layer is most predictable, when it is described as a "pure function" of the application state
  • Redux: the state mutations in app need to be described as a pure function of previous state and the action to return the next state (as a new Object [not modifying the previous object])
  • This function is called the reducer

Redux Store

const createStore = (reducer) => {
    let state;
    let listeners = [];

    const getState = () => state;

    const dispatch = (action) => {
        state = reducer(state, action);
        listeners.forEach(listener => listener());
    }

    const subscribe = (listener) => {
        listeners.push(listener);
        return () => {
            listeners = listeners.filter(l !== listener);
        }
    }

    dispatch({});

    return { getState, dispatch, subscribe };
};

Flux

Flux Implementations

Reflux

Reflux: Authentication-Flow

Create Actions

import Reflux from 'reflux';

export default Reflux.createActions({
    login: {children: ['completed', 'failed']},
    logout: {}
});

Create Stores

export default class StatusStore extends Reflux.Store {
    constructor() {
        super();
        this.state = {...}; // set the state vars
    },

    onLogin (email, password) {
        // send data to backend and authenticate
    },

    onLoginCompleted (authResponse) {
        // set the validated token and redirect to the requested page
    },

    onLogout () {
        // perform the right actions and remove the valid token
    }
}

Hook the Store to React Component

import AuthStore from '../stores/AuthStore';
import AuthActions from '../actions/AuthActions';

export default class Login extends React.Component {
    mixins: [
        Reflux.connect(AuthStore,"rkey"),
        Reflux.ListenerMixin
    ],

    componentDidMount () {this.listenTo(AuthStore, this._onAuthChange);},

    _onAuthChange(auth) {/* set the proper states and redirect */},

    _handleLogin(event) {AuthActions.login(entered_email, entered_password);},

    render() { return (/* form content to get login info, call _handleLogin() */);}
};

Router

module.exports = (
    <Route>
        <Route handler={Login} name="Login" path="Login"/>
        <Route handler={LoginRequired}>
            <Route handler={Master}>
                <DefaultRoute handler={Home} name="Home"/>
                <Route handler={Admin} name="Admin" path="admin">
            </Route>
        </Route>
    </Route>
);

There exist many more libraries listed in

https://github.com/enaqx/awesome-react

[highly recommend to check them out]

SpaceForward
Right, Down, Page DownNext slide
Left, Up, Page UpPrevious slide
POpen presenter console
HToggle this help