前言
網上別人的文檔都是 直接 就是上redux
redux-thunk
react-redux
,immutable
這樣的一套,這個有經驗的看還行,新手看就很吃力了,需要了解一步一步的安裝redux達到開發要求
我覺得這需要一個學習的過程,拔苗助長不是好事
這是我寫項目的逐步搭建的過程,歡迎查看源代碼github-pinduoduo
Redux
- 安裝redux(后面再安裝(react-redux)
因為
redux
是js的部分 所以不需要link
npm install redux--save
安裝完成后確認可以正常啟動
- 創建store
我的項目結構
和React項目一樣的項目結構
index.js
import { createStore } from 'redux'
import reducer from './reducer'
export default createStore(reducer) // 導入state
reducer.js
import actionTypes from './actionTypes'
const defaultState = { // 初始化state
data: 'my is redux!!!!'
}
export default (state = defaultState, action) => {
console.log(action)
if (action.type == actionTypes.CHANGE) { // 修改state
const newState = JSON.parse(JSON.stringify(state))
newState.data = 'change data!!!'
return newState
}
return state
}
actionTypes.js
export default {
CHANGE: 'change' // 定義統一的type
}
actionCreators.js
import actionTypes from './actionTypes'
export function change() { // 統一管理action
return {
type: actionTypes.CHANGE
}
}
最后在頁面里面
import React, { Component } from 'react'
import {
Text,
StyleSheet,
View,
StatusBar,
Dimensions,
Button
} from 'react-native'
import store from '../../../store/index' // 導入store
import { change } from '../../../store/actionCreators' // 導入action
export default class Popular extends Component {
constructor(props) {
super(props)
this.state = store.getState() // 初始化state,獲取redux里面數據
store.subscribe(() => { // 訂閱state的改變
this.setState(store.getState())
})
}
render() {
return (
<View>
<Text>{this.state.data}</Text>
<Button
title="更新state"
onPress={() => {
store.dispatch(change())
}}
/>
<Button
title="查看state"
onPress={() => {
console.log(store.getState())
}}
/>
</View>
)
}
}
const styles = StyleSheet.create({})
最基礎的redux
就使用成功了,但是這個還達不到我們的開發要求,下面將引入react-redux
Redux + React-redux
如果不了解
React-redux
,請學習后再說,不然肯定看不懂,React-redux文檔
之前我們在組件里面使用Redux直接去獲取數據,加入每個頁面都這樣寫,會很麻煩,所以我們要借助react-redux
來幫我們處理store
修改之前寫的頁面代碼,去掉之前頁面使用state
的地方
import React, { Component } from 'react'
import {
Text,
StyleSheet,
View,
StatusBar,
Dimensions,
Button
} from 'react-native'
import { change } from '../../../store/actionCreators'
class Popular extends Component {
render() {
return (
<View>
<Text>{this.props.data}</Text>
<Button title="更新state" onPress={() => {
//..
}} />
<Button
title="獲取state"
onPress={() => {
//..
}}
/>
</View>
)
}
}
const styles = StyleSheet.create({})
export default Popular
修改程序的掛載入口
index.js
/** @format */
import React, { Component } from 'react'
import { Provider } from 'react-redux'
import { AppRegistry } from 'react-native'
import store from './app/store'
import App from './app/routes/App'
import { name } from './app.json'
class Apps extends Component {
render() {
return (
// 掛載store,讓app內部所有組件都可以使用
<Provider store={store}>
<App />
</Provider>
)
}
}
AppRegistry.registerComponent(name, () => Apps)
這里我們就可以在組件里面通過connent
來接收了
import React, { Component } from 'react'
import { connect } from 'react-redux'
import {
Text,
StyleSheet,
View,
StatusBar,
Button
} from 'react-native'
import { change } from '../../../store/actionCreators'
class Popular extends Component {
render() {
return (
<View>
<StatusBar
translucent={true} // 設置沉浸式狀態欄 正常情況下 狀態欄高度為20 這里的20 需要頁面元素距離最上面 paddingTop:20
backgroundColor={'rgba(0,0,0,0.1)'} // 設置狀態欄顏色
animated={true} // 允許動畫切換效果
/>
<Text>{this.props.data}</Text>
<Button title="更新state" onPress={this.props.changeData} />
<Button
title="獲取state"
onPress={() => {
console.log(this.props.data)
}}
/>
</View>
)
}
}
const styles = StyleSheet.create({})
const mapState = state => ({
data: state.data
})
const mapDispatch = dispatch => ({
changeData() {
dispatch(change())
}
})
export default connect(
mapState,
mapDispatch
)(Popular)
這里我們React-redux
再次獲取並修改了redux里面的數據,相對之下,使用React-redux
后,頁面邏輯更加清楚
Redux + React-redux+immutable
immutable在日常開發里面很常見,讓我們的數據更加嚴謹
很簡單,首先安裝
npm install immutable
處理我們store的數據
import actionTypes from './actionTypes'
import {fromJS} from 'immutable'
const defaultState = fromJS({ // 將對象轉成immutable對象
data: 'my is redux!!!!'
})
export default (state = defaultState, action) => {
if (action.type == actionTypes.CHANGE) {
return state.set('data','change Redux!!!')
}
return state
}
然后處理我們頁面里面引用數據的地方
const mapState = state => ({
data: state.get('data') // immutable對象使用get獲取
})
redux的分離
將大量的store數據放在一起是非常不好的行為,我們要將每個組件之間的store
盡可能的分離
這里用的是redux給我們提供的 combineReducers 將store進行合並
修改頁面目錄結構,在頁面目錄里面創建store
組件內部的sore代碼
Popular/store/reducer
import actionTypes from './actionTypes'
import {fromJS} from 'immutable'
const defaultState = fromJS({
data: 'my is redux!!!!'
})
export default (state = defaultState, action) => {
if (action.type == actionTypes.CHANGE) {
return state.set('data','change Redux!!!')
}
return state
}
Popular/store/actionTypes
export default {
CHANGE: 'change'
}
Popular/store/actionCreators
import actionTypes from './actionTypes'
export function change() {
return {
type: actionTypes.CHANGE
}
}
Popular/store/index
import reducer from './reducer'
import actionCreators from './actionCreators'
import actionTypes from './actionTypes'
export { reducer, actionCreators, actionTypes }
// 使用入口
這樣我們就在組件內部新建了一個store,接下來我們要把組件內部的store合並store里面
./store/reducer
import { combineReducers } from 'redux'
import { reducer as homePopular } from '../src/home/Popular/store/index'
export default combineReducers ({
homePopular: homePopular
})
這就完成了store的合並,這里store變了,自然訪問就變了
Popular.js
const mapState = state => ({
data: state.homePopular.get('data')
})
最后引入redux
中間件
我一般情況下使用
redux-thunk
npm install redux-thunk
import { createStore,applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import reducer from './reducer'
export default createStore(
reducer,
applyMiddleware(thunk)
)
這里不做樣式了,會的人自然會,不會的學習一下,學會使用很簡單