Foreword
JavaScript is Redux state of the container, providing predictable of state management. In general, the relatively large size of the applet, page status, data caching, too many to manage things, the introduction of this time Redux can easily manage these states, the same data, a request, the application global shared.
The Taro also very kindly provided Redux transplant for developers.
To make it easier to use Redux
, Taro and provides react-redux
almost the same API package @tarojs/redux
to allow developers to achieve good development experience.
Redux to use global variables management
1, pages in the same directory new four folders.
store, actions, reducers, constants
store: Create a global single store.
actions: What used to describe the events occurred.
reducers: how to change the state tree for action.
constants: used to define the required action type
constants.
//store/index.js文件 import { createStore, applyMiddleware, compose } from 'redux' import thunkMiddleware from 'redux-thunk' import rootReducer from '../reducers' const composeEnhancers = typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize... }) : compose const middlewares = [ thunkMiddleware ] if (process.env.NODE_ENV === 'development') { middlewares.push(require('redux-logger').createLogger()) } const enhancer = composeEnhancers( applyMiddleware(...middlewares), // other store enhancers if any ) export default function configStore () { const store = createStore(rootReducer, enhancer) return store }
First, app.js
the introduction of a defined start store
, using the @tarojs/redux
provided Provider
assembly foregoing written store
access application in this way, the Provider
parcels to the application can share the page store
.
//app.js
import Taro, { Component } from '@tarojs/taro' import { Provider } from '@tarojs/redux' import configStore from './store' import Index from './pages/index' import './app.scss' const store = configStore() class App extends Component { ... render () { return ( <Provider store={store}> <Index /> </Provider> ) } }
constants
Folder to define a set of required action type
constants
//constants/login.js export const LOGIN_TYPE = "login_type"
Then start creating processing instruction reducers
.
// reducers/index.js import { combineReducers } from 'redux' import login from "./login" export default combineReducers({ login })
// reducers/login.js import {LOGIN_TYPE} from "../constants/login" const INITIAL_STATE = { loginType: false } export default function login (state = INITIAL_STATE, action) { switch (action.type) { case LOGIN_TYPE: return { ...state, loginType: action.data } default: return state } }
Then the actions
instruction corresponding to the defined function.
//actions/login.js import {LOGIN_TYPE} from "../constants/login" export const loginTypeFun = (data) => { return { type: LOGIN_TYPE, data: data } }
The last change in state for data
//pages/index/index.js import Taro, { Component } from '@tarojs/taro' import { View } from '@tarojs/components' import { AtButton } from "taro-ui" import './index.scss'
import { connect } from '@tarojs/redux'
import { loginTypeFun } from '../../actions/login'
@connect(({ login }) => ({ login }), (dispatch) => ({ changeLoginType(data) { dispatch(loginTypeFun(data)}) } })) class Index extends Component { constructor () { super(...arguments) this.state = ({ }) } config = { navigationBarTitleText: '首页' } componentWillMount() { } toChangeLogin = (e) => { this.props.changeLoginType(true) } } render () { return ( <View className='index'> {this.props.login.loginType} <AtButton type="primary" onClick={this.toChangeLogin}>改变状态loginType</AtButton> </View> ) } } export default Index
The output true