React实战篇(React仿今日头条)
发布时间:2020-12-15 06:30:21 所属栏目:百科 来源:网络整理
导读:前言 上次初学用 react 写了个后台管理,这次便寻思写个移动端的项目。便有了这次的这个项目。 这个项目以前写了个 vue 的版本。有兴趣的可以 点击进入 模拟数据用的是 Easy Mock 用的是我以前写 vue-toutiao 用到的数据 账号: vue-toutiao 密码: 123456 技
前言上次初学用 这个项目以前写了个 模拟数据用的是 Easy Mock 账号: vue-toutiao 技术栈
结构:
效果演示
知识点按需加载通过 // 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 } } ) 如下使用 import asyncComponent from './asyncComponent' const Demo = asyncComponent(() => import(`views/demo.js`)) <Route path="/demo" component={Demo}/> 路由设置统一配置路由,及路由状态 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') } ] 登录拦截通过路由配置中 { path: '/system',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}/> }) 路由动画通过 然后通过 history.slideStatus 来判断如何动画 react-router-transition 具体API redux-thunk处理action异步用 // 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 上查看。 github (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |