基于React、Redux、Webpack 和 React-Router的项目模板。

Lucian

特性

  • 快速上手,没有其它cli这么多概念,只要会React、Redux、Webpack、React-Router,快速搭建中后台管理平台。
  • 路由匹配,包含url输入、js跳转、菜单切换。
  • Action,不须要重复定义action,好比等待Action、成功Actoin、失败Action。写更少的action,完成更多的事。
  • 自定义中间件,帮助Action完成异步操做。
  • immutable,更简洁,持久化数据结构。
  • 简单实现combineReducers,监听当前正在触发的action。
  • 彻底自定义的cli,内置redux、webpack、react-router、classnames、dayjs、eslint等。

例子

是否可用于生产环境?

固然!公司内用于生产环境的项目估计已经有 3+ 。html

是否支持 IE8 ?

不支持。react

快速上手

建立新应用

$ git clone https://github.com/Cherry-Team/lucian.git

$ cd lucian

$ npm install

$ npm start

几秒以后,你将会看到如下输出webpack

使用 antd

import React, { Component } from 'react';
import { Button } from 'antd';
class Index extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <Button>Button</Button>
        );
    }
}

export default Index;

定义路由

咱们要写个应用来显示列表,首先是建立路由。git

新建 route component pages/List.js,内容以下:github

import React from 'react';

const List= (props) => (
  <h2>List</h2>
);

export default List;

添加路由信息到路由表,编辑 routeConfig.js web

const List = lazy(() => import(/* webpackChunkName: "List"*/'./pages/List/index'));
const routes = [
    {
        path: '/list',
        component: List
    }
];

编写 UI Component

随着应用的发展,你会须要在多个页面分享 UI 元素 (或在一个页面使用屡次),根目录下新建components/LayoutHeader/index.jsxnpm

import React, { Component } from 'react';
import { Avatar, Dropdown, Menu } from 'antd';

const menu = (
    <Menu>
        <Menu.Item>
            <a target="_blank" rel="noopener noreferrer" href={false}>
                退出
            </a>
        </Menu.Item>
    </Menu>
);

class Index extends Component {
    constructor(props) {
        super(props);
        this.state = {};
    }

    render() {
        return (
            <section>
                <div>
                    订单系统
                </div>
                <div>
                    <span>消息</span>
                    <Dropdown overlay={menu}>
                        <div>
                            <Avatar size={28} icon="user" />
                            <span >Faker</span>
                        </div>
                    </Dropdown>
                </div>
            </section>
        );
    }
}

export default Index;

使用 Redux 完成计数器

新建pages/Counter/index.jsxredux

import React, { Component } from 'react';
import { connect } from 'react-redux';
import * as action from '../../actions/counter';
import './index.less';

const mapStateToProps = state => {
    const { counter } = state;
    return {
        counter: counter.toJS()
    };
};

const mapDispatchToProps = (dispatch) => ({
    add: (...args) => dispatch(action.add(...args)),
    reduce: (...args) => dispatch(action.reduce(...args))
});

class Index extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <section className="counter">
                <button onClick={() => this.props.add(this.props.counter.count)}>+</button>
                <span>count is: {this.props.counter.count}</span>
                <button onClick={() => this.props.reduce(this.props.counter.count)}>-</button>
            </section>
        );
    }
}

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

建立actions/counter.jsantd

export const ADD = 'add';
export function add(params) {
    return {
        type: ADD,
        params
    };
}

export const REDUCE = 'reduce';
export function reduce(params) {
    return {
        type: REDUCE,
        params
    };
}

而后新建reducers/counter.js数据结构

import { fromJS } from 'immutable';
import { createReducer } from 'redux-immutablejs';
import {
    ADD,
    REDUCE
} from './../actions/counter';

const initialState = fromJS({
    count: 0
});

export default createReducer(initialState, {
    [ADD]: (state, { params }) => {
        return state.set('count', params + 1);
    },
    [REDUCE]: (state, { params }) => {
        return state.set('count', params - 1);
    }
});

而后在reducers/rootReducers.js中引入

// reducers配置文件
import { routerReducer } from 'react-router-redux';
import orderList from './orderList';
import counter from './counter';

// 保存当前正在执行的action type
const combineReducers = (reducers) => {
    return (state = {}, action) => {
        return Object.keys(reducers).reduce((nextState, key) => {
            nextState[key] = reducers[key](state[key], action);
            return nextState;
        }, { actionType: action.type });
    };
};

const rootReducers = combineReducers({
    counter,
    orderList,
    router: routerReducer
});
export default rootReducers;

构建应用

完成开发而且在开发环境验证以后,就须要部署给咱们的用户了。先执行下面的命令:

$ npm run build

几秒后,输出如下内容:

build 命令会打包全部的资源,包含 JavaScript, CSS, web fonts, images, html 等。而后你能够在 dist/ 目录下找到这些文件。

源码

Lucian

将来

  • jsx升级TypeScript
  • ant-design 4.0.0
  • webpack 5.0.0
  • React 17.0.0
相关文章
相关标签/搜索