React third-party component 4 (the use of Reflux for state management ②TodoList)

React third-party component 4 (the use of Reflux for state management ②TodoList)

This tutorial has a total of 5 articles, one updated daily, please follow us! You can enter the historical news to view the previous articles, and stay tuned for our new articles!

1. React third-party component 4 (the use of Reflux for state management①simple use) ---2018.03.13

2. React third-party component 4 (Use of Reflux for state management ②TodoList) ---2018.03.14

3. React third-party component 4 (use of Reflux for state management ③TodoList) ---2018.03.15

4. React third-party components 4 (the use of Reflux for state management under TodoList) ---2018.03.16

5. React third-party components 4 (use of Reflux for state management ⑤ asynchronous operation) ---2018.03.19

Development environment: Windows 8, node v8.9.1, npm 5.5.1, WebStorm 2017.2.2

This tutorial has a total of 5 articles, one updated daily, please follow us! You can enter the historical news to view the previous articles, and stay tuned for our new articles!

1. React third-party component 4 (the use of Reflux for state management①simple use) ---2018.03.13

2. React third-party component 4 (Use of Reflux for state management ②TodoList) ---2018.03.14

3. React third-party component 4 (use of Reflux for state management ③TodoList) ---2018.03.15

4. React third-party components 4 (the use of Reflux for state management under TodoList) ---2018.03.16

5. React third-party components 4 (use of Reflux for state management ⑤ asynchronous operation) ---2018.03.19

Development environment: Windows 8, node v8.9.1, npm 5.5.1, WebStorm 2017.2.2

1. We make a copy of reflux1 to reflux2

2. Modify the Index.jsx file under reflux

import React from'react';
import {HashRouter, Route, NavLink, Redirect} from'react-router-dom';
import ReFlux1 from'./reflux1/Index'
import ReFlux2 from'./reflux2/Index'

const Index = ({match}) =>
    <HashRouter>
        <div>
            <div className="nav">
                <NavLink to="/ReFlux/ReFlux1" activeClassName="selected">ReFlux1</NavLink>
                <NavLink to="/ReFlux/ReFlux2" activeClassName="selected">ReFlux2</NavLink>
            </div>
            <Route exact path={`${match.url}`}
                   render={() => (<Redirect to={`${match.url}/ReFlux1`}/>)}/>
            <Route path={`${match.url}/ReFlux1`} component={ReFlux1}/>
            <Route path={`${match.url}/ReFlux2`} component={ReFlux2}/>
        </div>
    </HashRouter>
;

export default Index;

3. Modify Index.jsx under reflux2

import React from'react'
import Reflux from'reflux'
import Action from'./Action'
import Store from'./Store'

class Index extends Reflux.Component {
    constructor(props) {
        super(props);
        this.store = Store;
    }

    render() {
        let list = this.state.list;
        return (
            <div className="todoList">
                <input type="text" ref="todoInput"/>
                <button onClick={() => Action.addTodo(this.refs['todoInput'].value)}>Add
                </button>
                <div>
                    {
                        list.length> 0 && list.map(data =>
                            <li key={data.id}>
                                {data.title}
                            </li>
                        )
                    }
                </div>
            </div>
        );
    }
}

export default Index;

4. Modify Action.js

import Reflux from'reflux'

let Action = Reflux.createActions([
    'addTodo'
]);
export default Action;

5. Modify Store.js

import Reflux from'reflux'
import Action from'./Action'

class Store extends Reflux.Store {
    constructor() {
        super();
        this.listenables = Action;
        this.state = {
            list: []
        }
    }

    onAddTodo(title) {
        if (!title) {
            alrt('Content cannot be empty');
        }else {
            let list = this.state.list;
            list.push({id: list.length + 1, title: title, status: 1});
            this.setState({list: list});
        }

    }
}

export default Store;

6. Look at the browser

Reference: https://cloud.tencent.com/developer/article/1092666 React third-party component 4 (Use of Reflux for state management ②TodoList)-Cloud + Community-Tencent Cloud