redux那点事

一个简单demo,仅供参考
1.APP.js

import React from 'react';
import { Provider } from 'react-redux';
import Counter from './src/containers/Counter';
import configureStore from './src/configureStore';

const store = configureStore();

export default () => (
  
    
  
);

2.src/configtureStore.js

    
/* eslint global-require: 0 */
import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';
import reducer from './reducers';

const middlewares = [thunk];
const enhancer = composeWithDevTools({
  // Options: https://github.com/jhen0409/react-native-debugger#options
})(applyMiddleware(...middlewares));

export default function configureStore(initialState) {
  const store = createStore(reducer, initialState, enhancer);
  if (module.hot) {
    module.hot.accept(() => {
      store.replaceReducer(require('./reducers').default);
    });
  }
  return store;
}

3.src/actions/counter.js

export const increment = () => ({
  type: 'increment',
});

export const decrement = () => ({
  type: 'decrement',
});

export const incrementIfOdd = () => (dispatch, getState) => {
  const { counter } = getState();

  if (counter % 2 === 0) {
    return;
  }

  dispatch(increment());
};

export const incrementAsync = (delay = 1000) => dispatch =>
  setTimeout(() => {
    dispatch(increment());
  }, delay);

4.src/actions/containers/Counter.js

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { StyleSheet, View, Text, TouchableHighlight } from 'react-native';

import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as CounterActions from '../actions/counter';

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  text: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
});

@connect(
  state => state,
  dispatch => bindActionCreators(CounterActions, dispatch)
)
export default class Counter extends Component {
  static propTypes = {
    incrementAsync: PropTypes.func.isRequired,
    increment: PropTypes.func.isRequired,
    decrement: PropTypes.func.isRequired,
    incrementIfOdd: PropTypes.func.isRequired,
    counter: PropTypes.number.isRequired,
  };

  constructor(props) {
    super(props);
    const { incrementAsync } = props;
    this.incrementAsync = () => incrementAsync();
  }

  render() {
    const { increment, decrement, incrementIfOdd, counter } = this.props;
    return (
      
        Clicked: {counter} times
        
          +
        
        
          -
        
        
          Increment if odd
        
        
          Increment async
        
      
    );
  }
}

5.src/reducers/counter.js

const initialState = 0;

const actionsMap = {
  increment(state) {
    return state + 1;
  },
  decrement(state) {
    return state - 1;
  },
};

export default (state = initialState, action) => {
  const reduceFn = actionsMap[action.type];
  if (!reduceFn) return state;
  return reduceFn(state, action);
};

5.src/reducers/index.js

import { combineReducers } from 'redux';
import counter from './counter';

export default combineReducers({
  counter,
});

欢迎交流,等你的建议

你可能感兴趣的:(react-native)