成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

React實戰(zhàn)篇(React仿今日頭條)

NicolasHe / 946人閱讀

摘要:前言上次初學(xué)用寫了個后臺管理,這次便尋思寫個移動端的項目。便有了這次的這個項目。然后通過來判斷如何動畫具體處理異步用來書寫跟配置還有一些零零散散的知識點,就不介紹了,具體可以到上查看。個人博客在線觀看地址

前言

上次初學(xué)用 react 寫了個后臺管理,這次便尋思寫個移動端的項目。便有了這次的這個項目。

這個項目以前寫了個 vue 的版本。有興趣的可以 點擊進入

模擬數(shù)據(jù)用的是 Easy Mock
用的是我以前寫 vue-toutiao 用到的數(shù)據(jù)

賬號: vue-toutiao
密碼: 123456

技術(shù)棧

react + react-redux + react-router + webpack

結(jié)構(gòu):

build: webpack配置

config: 項目配置參數(shù)

src
actions: 存放 action 方法
assets: 靜態(tài)資源文件,存放圖片啥的
components: 常用組件
reducers: 存放 reducer
router: 路由管理
store: 狀態(tài)管理 redux
styles: 樣式文件
utils: 常用封裝
views: 視圖頁面

static: 靜態(tài)文件: 存放 favicon.ico 等等

效果演示

知識點 按需加載

通過 import() 方法加載組件, 在通過高階組件處理 import 返回的 Promise 結(jié)果。

// 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 `);
                throw err
            }
        }

        render() {
            const { Component } = this.state
            return (Component) ?  : null
        }
    }
)

如下使用

import asyncComponent from "./asyncComponent"
const Demo = asyncComponent(() => import(`views/demo.js`))
路由設(shè)置

統(tǒng)一配置路由,及路由狀態(tài)

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: "系統(tǒng)設(shè)置",
        auth: true, 
        component: _import_views("System")
    }
] 
登錄攔截

通過路由配置中 auth 屬性來判斷是否需要登錄
如以下配置

{ 
    path: "/system", 
    name: "系統(tǒng)設(shè)置",
    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 
        }
        return 
    }
}


// 生成route
const renderRouteComponent = routes => routes.map( (route, index) => {
    if (route.auth) { // 需要權(quán)限登錄
        return 
    }
    return 
})
路由動畫

通過 react-router-transition 做的切換動畫。

然后通過 history.slideStatus 來判斷如何動畫

react-router-transition 具體API

redux-thunk處理action異步

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 上查看。
github
個人博客
在線觀看地址

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/94070.html

相關(guān)文章

  • 2017-08-01 前端日報

    摘要:前端日報精選掌握開發(fā)工具新一代前端開發(fā)技術(shù)和到底是咋回事第期深入淺出高階組件基于的移動頁面緩存解決方案譯保護我們的,立刻停止狐步舞中文譯中和之間的區(qū)別個人文章譯什么是個人文章譯個人文章熱身實戰(zhàn)過渡與動畫實現(xiàn)炫酷下拉, 2017-08-01 前端日報 精選 掌握Chrome開發(fā)工具:新一代前端開發(fā)技術(shù)exports、module.exports和export、export default...

    gclove 評論0 收藏0
  • 重寫GridView實現(xiàn)仿今日頭條的頻道編輯頁(1)

    摘要:但由于這里僅僅是實現(xiàn)一個,因此存儲功能僅通過一個單例類來模擬實現(xiàn)。 本文旨在通過重寫GridView,配合系統(tǒng)彈窗實現(xiàn)仿今日頭條的頻道編輯頁面 注:由于代碼稍長,本文僅列出關(guān)鍵部分,完整工程請參見【https://github.com/G9YH/YHChannelEdit】 在開始講解盜版的實現(xiàn)方案前,讓我們先來看看正版與盜版的實際使用效果對比,首先是正版 showImg(https:...

    張憲坤 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<