上次初学用 react
写了个后台管理,此次便寻思写个移动端的项目。便有了此次的这个项目。html
这个项目之前写了个 vue
的版本。有兴趣的能够 点击进入vue
模拟数据用的是 Easy Mockreact
用的是我之前写 vue-toutiao 用到的数据webpack
帐号: vue-toutiaoios
密码: 123456git
react
+ react-redux
+ react-router
+ webpack
github
actions
: 存放 action 方法assets
: 静态资源文件,存放图片啥的components
: 经常使用组件reducers
: 存放 reducerrouter
: 路由管理store
: 状态管理 reduxstyles
: 样式文件utils
: 经常使用封装views
: 视图页面经过 import()
方法加载组件, 在经过高阶组件处理 import
返回的 Promise
结果。web
// asyncComponent.js import React from 'react' export default loadComponent => ( class AsyncComponent extends React.Component { state = { Component: null, } async componentDidMount() { if (this.state.Component !== null) return try { const {default: Component} = await loadComponent() this.setState({ Component }) }catch (err) { console.error(`Cannot load component in <AsyncComponent />`); throw err } } render() { const { Component } = this.state return (Component) ? <Component {...this.props} /> : null } } ) 复制代码
以下使用npm
import asyncComponent from './asyncComponent' const Demo = asyncComponent(() => import(`views/demo.js`)) <Route path="/demo" component={Demo}/> 复制代码
统一配置路由,及路由状态redux
import asyncComponent from './asyncComponent' const _import_views = file => asyncComponent(() => import(`views/${file}`)) export const loyoutRouterMap = [ { path: '/', name: '首页', exact: true, component: _import_views('Home') }, { path: '/video', name: '视频', component: _import_views('Video') }, { path: '/headline', name: '微头条', component: _import_views('Headline') }, { path: '/system', name: '系统设置', auth: true, component: _import_views('System') } ] 复制代码
经过路由配置中 auth
属性来判断是否须要登陆 如如下配置
{ path: '/system', name: '系统设置', auth: true, component: _import_views('System') } 复制代码
登录配置及判断
// authRoute.js import React from 'react' import store from '../store' import { Route, Redirect } from 'react-router-dom' export default class extends React.Component { render () { let {component: Component, ...rest} = this.props // 是否登陆 if (!store.getState().user.user.name) { return <Redirect to='/login' /> } return <Route {...rest} component={Component}/> } } // 生成route const renderRouteComponent = routes => routes.map( (route, index) => { if (route.auth) { // 须要权限登陆 return <AuthRoute key={index} {...route}/> } return <Route key={index} {...route}/> }) 复制代码
经过 react-router-transition
作的切换动画。
而后经过 history.slideStatus 来判断如何动画
用 redux-actions
来书写 action 跟 reducer
// action.js import { createAction } from 'redux-actions' import axios from 'utils/axios' export const getHeadlineList = (params) => dispatch => { return new Promise( (resolve, reject) => { axios.get('headline/list', params) .then( res => { const list = res.data.list dispatch(createAction('GET_HEADLINE_LIST')(list)) resolve(list) }).catch( err => { reject(err) }) }) } // reducer.js import { handleActions } from 'redux-actions' import { combineReducers } from 'redux' const state = { headlineList: [] } const headline = handleActions({ GET_HEADLINE_LIST: (state, action) => { let list = action.payload state.headlineList = state.headlineList.concat(list) return {...state} } }, state) export default combineReducers({ headline }) // store.js // redux-thunk配置 import { createStore, compose, applyMiddleware } from 'redux' import reducer from '../reducers' import thunk from 'redux-thunk' const configureStore => createStore( reducer, compose( applyMiddleware(thunk) ) ) export default configureStore() 复制代码
还有一些零零散散的知识点,就不介绍了,具体能够到 github 上查看。