Router Demo

Action

import {v4} from 'uuid';
import Constants from "../constants";

export const addItem = (title, color) => ({
    type: Constants.ADD_ITEM,
    id: v4(),
    title,
    color
})

funcs

import {compose} from "redux";

const getFirstArrayItem = array => array[0]

const filterArrayById = (array, id) =>
    array.filter(item => item.id === id)

export const findById = compose(
    getFirstArrayItem,
    filterArrayById
)

Reducers

import Constants from "../constants";

const item = (state = {}, action) => {
    switch (action.type) {
        case Constants.ADD_ITEM:
            return {
                id: action.id,
                title: action.title,
                color: action.color
            }
        default:
            return state;
    }
}

export const items = (state = [], action) => {
    switch (action.type) {
        case Constants.ADD_ITEM:
            return [
                ...state,
                item({}, action)
            ]
        default:
            return state;
    }
}

Stores

import {createStore, combineReducers} from "redux";
import {items} from "../reducers";

const store = createStore(combineReducers({items}));

export default store;

Constants

const Constants = {
    ADD_ITEM: 'ADD_ITEM'
}

export default Constants;

AddItemForm

import React from 'react';
import {connect} from 'react-redux';
import {addItem} from "./actions";

const AddItemForm = ({redux_addItem}) => {

    let title, color;

    const submit = e => {
        e.preventDefault();
        redux_addItem(title.value, color.value);
        title.value = '';
        color.value = '';
        title.focus();
    }

    return (
        <form onSubmit={submit}>
            <input type="text"
                   ref={input => title = input}/>
            <input type="color"
                   ref={input => color = input}/>
            <button>ADD</button>
        </form>
    )
}

function mapDispatchToProps(dispatch) {
    return {
        redux_addItem: (title, color) => {
            dispatch(addItem(title, color))
        }
    }
}

export default connect(
    null,
    mapDispatchToProps
)(AddItemForm);

ItemList

import React from 'react';
import Item from "./Item";
import {connect} from "react-redux";

const ItemList = ({items}) => {
    return (
        <ul>
            {
                items.length === 0 ?
                    <p>No Item List. (Add a Item)</p> :
                    items.map((item, index) =>
                        <Item key={item.id}
                              {...item}/>
                    )
            }
        </ul>
    )
}

export default connect(
    (state)=> ({items: state.items}),
    null
)(ItemList);

Item

import React from 'react';
import {withRouter} from "react-router-dom";

const Item = ({id, title, history}) => {
    return (
        <li
            onClick={() => history.push(`/${id}`)}>
            {title}
        </li>
    )
}

export default withRouter(Item);

ItemDetial

import React from 'react';
import {connect} from "react-redux";
import {findById} from "./funcs";

const ItemDetail = ({title, color, history}) => {
    return (
        <div style={{
            height: 100,
            width: 200,
            backgroundColor: color
        }}
             onClick={() => history.goBack()}>
            <h1>{title}</h1>
            <h1>{color}</h1>
        </div>
    )
}

export default connect(
    (state, props) => findById(
        state.items,
        props.match.params.id
    )
)(ItemDetail)

App

import React from 'react';
import {Switch, Route} from 'react-router-dom';
import AddItemForm from "./AddItemForm";
import ItemList from "./ItemList";
import ItemDetail from "./ItemDetail";

const App = () => {

    return (
        <Switch>
            <Route extra path="/:id" component={ItemDetail}/>
            <Route path="/"
                   component={() => (
                       <div>
                           <AddItemForm/>
                           <ItemList/>
                       </div>
                   )}/>
        </Switch>
    )

}

export default App;

Index

import React from 'react';
import {render} from "react-dom";
import store from "./chapter11-router-test01/stores";
import {HashRouter} from "react-router-dom";
import {Provider} from "react-redux";
import App from "./chapter11-router-test01/App";

render(
    <Provider store={store}>
        <HashRouter>
            <App/>
        </HashRouter>
    </Provider>,
    document.getElementById('root')
)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值