摘要:的使用用戶發(fā)出函數(shù)算出新的重新渲染三大原則單一數(shù)據(jù)源,利用的形式向下傳播數(shù)據(jù)流決定只讀,通過修改通過純函數(shù)來修改組件狀態(tài),是描述動(dòng)作的純函數(shù)連接和基于全局的,選擇我們要注入的不同的組件分開把注入,讀取方法三劍客先確定一下初始狀
redux的使用
三大原則react-native, react, react-redux, react-navigaition, redux-thunk, redux-persist
用戶發(fā)出action, reducer函數(shù)算出新的state, view重新渲染
單一數(shù)據(jù)源,利用props的形式向下傳播(react數(shù)據(jù)流決定)
state只讀,通過action修改
通過純函數(shù)reducer來修改組件狀態(tài),reducer是描述action動(dòng)作的純函數(shù)
react-redux 連接react和reduximport { connect, Provider } from "react-redux"connet([mapStateToProps], [mapDispatchToProps], [mergeProps])(App)
mapStateToProps: 基于全局的state,選擇我們要注入的props
不同的組件分開connec
function select(state){ return{ // 把state.todos注入,讀取方法: this.props.visibleTodo visibleTodo: state.todos } } const todoApp connect(select)(App) // export default todoApp
// store = "balabala"redux三劍客
// 先確定一下初始狀態(tài)(狀態(tài)表示的格式) export default { money: 0, lastJob:"no job" }ACTION
定義動(dòng)作,比如搶劫銀行、搬磚,但是怎么搶怎么搬還是reducer來定
/** * Created by liuyiman on 2017/7/3. */ // 定義一個(gè)menoy+的action function addmoney(money){ return { // 可以用一個(gè)文件管理type,之所以是type是因?yàn)槲业膔educer要根據(jù)這個(gè)來判斷 type: "ADD_MONEY", money } } function setLastJob(job){ return { type: "SET_JOB", job } } // 定義一個(gè)賺錢的方式1 ,搶劫銀行 export function robBank(){ return (dispatch, getState) => { // 賺一百萬 let { earn } = getState() dispatch(addmoney(earn.money + 1000000)) return dispatch(setLastJob("robber")) } } // 定義一個(gè)賺錢的方式2,建材轉(zhuǎn)移者 export function moveBrick(){ return (dispatch, getState) => { console.log("s",getState()) let { earn } = getState() // 賺一塊錢 dispatch(addmoney( earn.money + 1 )) dispatch(setLastJob("brick mover")) } } // 破產(chǎn),數(shù)據(jù)清零 export function goBroke() { return { type: "BROKE", money: 0, job:"broke" } }reducers
描述action如何改變store(in fact state)
import { combineReducers } from "redux" import initializeState from "./initializeState" // 定義一個(gè)reducer function earn( state = initializeState, action ) { switch (action.type) { case "ADD_MONEY": return{ ...state, money:action.money } case "SET_JOB": return{ ...state, lastJob:action.job } case "BROKE": return{ ...state, money:action.money, lastJob:action.job } default: return state } } export default earnstore
不同的組件可以 connet 到不同的 store
import { createStore, applyMiddleware, compose, combineReducers } from "redux" import thunkMidlleware from "redux-thunk" import earn from "./reducers" import { persistStore, autoRehydrate} from "redux-persist" import { AsyncStorage } from "react-native" // base reducer let baseReducers = { earn: earn } /* * 考慮到后面要將react-navigation的reducer加進(jìn)來,使用redux-persist,所以寫了這個(gè)helper * const store = configStore(reducers)() * */ const configStore = function (reducers = {}) { const rootReducer = combineReducers({ ...baseReducers, ...reducers }) return function (_options = {}) { const store = createStore( rootReducer, _options.initialState, compose( applyMiddleware(thunkMidlleware), autoRehydrate() ) ) const options = { storage: AsyncStorage, blacklist: _options.blacklist } persistStore(store, options) return store } } export default configStoremore 中間件 middleware
對(duì)store.dispatch的重新定義
在發(fā)出action和執(zhí)行reducer之間,添加了其他功能
redux-thunk使dispatch可以接受函數(shù)說作為參數(shù),使異步的action可以被觸發(fā)
// 無論killSomeOne是Action create還是普通的返回{}的action this.props.dispath(killSomeOne("vincent"))redux-persist
本地保存store狀態(tài)(react-native 本地緩存),可以設(shè)置白名單黑名單自動(dòng)保存等等,特別好用
/* * 考慮到后面要將react-navigation的reducer加進(jìn)來,使用redux-persist,所以寫了這個(gè)helper * const store = configStore(reducers)() * */ const configStore = function (reducers = {}) { const rootReducer = combineReducers({ ...baseReducers, ...reducers }) return function (_options = {}) { const store = createStore( rootReducer, _options.initialState, compose( applyMiddleware(thunkMidlleware), autoRehydrate() ) ) const options = { storage: AsyncStorage, blacklist: _options.blacklist } persistStore(store, options) return store } } export default configStorereact-navigation + redux
import React, { Component } from "react"; import { AppRegistry, StyleSheet, Text, View, Button } from "react-native"; // navigation import { TabNavigator, addNavigationHelpers, StackNavigator } from "react-navigation" // for connect redux and react import { Provider,connect } from "react-redux" import configStore from "./redux/store" // import actions import {moveBrick, robBank, goBroke} from "./redux/actions" // initialState import initialState from "./redux/store" // 賺錢頁面 class Earn extends Component { constructor(props) { super(props) } render(){ const { earn, dispatch } = this.props console.log( "saa",initialState,earn ) return() } } // 傳入 earn頁面的redux const earnSelect = function (state) { return{ earn: state.earn } } // 連接,吧earn上面的select傳入earn的props里面 const ConnnetedEarn = connect(earnSelect)(Earn) // tab navigation的另一個(gè)頁面 class Screen extends Component{ constructor(props){ super(props) } render() { const { navigate } = this.props.navigation return( 先賺一個(gè)億! my money$:{earn.money} my last job:{earn.lastJob} ) } } // 注冊(cè)一個(gè)tag navigator const AppNav = TabNavigator({ "earn":{ screen: ConnnetedEarn }, "screen": { screen: Screen } }) // stack navigation 的頁面 class Stack2 extends Component { constructor(props){ super(props) } render(){ const {navigate} = this.props.navigation return ( SCREEN! {navigate("Stack2")}}/> ) } } /* * 注冊(cè)stack * 一個(gè)是 上面的 tag navigation的頁面 * 另一個(gè)是 上面的 stack2 * */ const StackNav = StackNavigator({ App:{ screen: AppNav, title: "app" }, Stack2: { screen: Stack2, title: "stack2" } }) const navInitialState = StackNav.router.getStateForAction(AppNav.router.getActionForPathAndParams("screen")) const navReducer = (state = navInitialState, action) => { console.log("state:",state) let nextState = StackNav.router.getStateForAction(action, state); console.log("action", action) return nextState || state } /* * 加入navReducer,生成store * */ const store = configStore({ nav: navReducer })({ blacklist:["nav"] }) /* * stack app * 利用addNavigationHelper吧navigation傳進(jìn)去 * */ class App extends Component{ render(){ return( Stack2 navigate("screen")}/> ) } } /* * 把nav傳進(jìn)去 * */ function select(state) { console.log("state",state) return { nav: state.nav } } /* * connect connect! * */ const ConnectedApp = connect(select)(App) // 加上 provider和store class reduxLearn extends Component { render() { return ( ); } } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: "center", alignItems: "center", backgroundColor: "#F5FCFF", }, welcome: { fontSize: 20, textAlign: "center", margin: 10, }, instructions: { textAlign: "center", color: "#333333", marginBottom: 5, }, }); // 注冊(cè) AppRegistry.registerComponent("reduxLearn", () => reduxLearn);
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/83870.html
摘要:安卓系統(tǒng)的信息系統(tǒng)的信息項(xiàng)目部分截圖項(xiàng)目源代碼公共組件首頁所有圖片登錄頁我的頁新聞頁配置省市區(qū)文件進(jìn)入輪播頁項(xiàng)目入口文件項(xiàng)目依賴配置最新生成的文件信息上次打包的版本路由配置文件 github地址:https://github.com/cryingbat/... react-native-app use react-native。Pack of the generated apk ins...
摘要:從到完美,用和寫一個(gè)在年開源了后,緊接著在年就又開源了,就此打開了用和前端技術(shù)寫原生之路。對(duì)應(yīng)來看,程序只有兩個(gè)部分,和,而部分則分為組件和。所以,在端的純庫無在中同樣適用,如等。應(yīng)用實(shí)例便是使用開發(fā)的一個(gè)日記。 從 1 到完美,用 js 和 react-native 寫一個(gè) APP facebook 在 2013 年開源了 react 后,緊接著在 2015 年就又開源了 react...
摘要:之前寫了一篇沒有加入的的小博文。一拆分結(jié)構(gòu)根據(jù)自己的習(xí)慣和固定套路,拆分目錄結(jié)構(gòu)和組件結(jié)構(gòu)。把的導(dǎo)航組件集中放在純粹是個(gè)人習(xí)慣。二代碼實(shí)現(xiàn)入口文件是用來做的數(shù)據(jù)持久化。添加事項(xiàng)后要通知其他組件更新數(shù)據(jù)。 讀前須知 這個(gè)項(xiàng)目是第一次使用Redux的實(shí)例,并不具有專業(yè)性的理論知識(shí)。純粹分享一次開發(fā)過程與心得。之前寫了一篇沒有加入Redux的React Native ToDoList的小博文...
摘要:鑒于我平常使用的是系統(tǒng),就決定我只開發(fā)安卓客戶端,客戶端我們又招了一個(gè)小伙伴。一般來講,安卓系統(tǒng)是這一兩年市面上常用機(jī)型配置的系統(tǒng)。 從去年的10月份開始,我的大部分工作重心從傳統(tǒng)的前端開發(fā)轉(zhuǎn)向了使用ReactNative開發(fā)APP,在這個(gè)過程當(dāng)中,走過了不少彎路,也遇到了一些技術(shù)相關(guān)的問題,但總算沒有辜負(fù)那些對(duì)我信任的人。經(jīng)歷過痛苦和無助,終于堅(jiān)持了下來,一個(gè)月的時(shí)間把產(chǎn)品成功部署上...
摘要:比如安卓平臺(tái)在中不能觸屏手動(dòng)滾動(dòng)。主要的功能界面模塊大部分主要是展示的,網(wǎng)易云的頁面和功能實(shí)在是太多了,由于時(shí)間關(guān)系,并沒有把所有的功能都做完整,后續(xù)會(huì)陸續(xù)加上其他的功能。各部分模塊首頁展示音樂播放,包括動(dòng)畫,歌詞同步等。 React Native 模仿網(wǎng)易云音樂手機(jī)客戶端,兼容安卓和IOS兩個(gè)平臺(tái)。 GitHub 完整源碼地址https://github.com/yezihaohao...
閱讀 3038·2023-04-25 18:06
閱讀 3307·2021-11-22 09:34
閱讀 2869·2021-08-12 13:30
閱讀 2058·2019-08-30 15:44
閱讀 1669·2019-08-30 13:09
閱讀 1638·2019-08-30 12:45
閱讀 1726·2019-08-29 11:13
閱讀 3617·2019-08-28 17:51