// all action String Type will be exported from here
export const INCREASE_BURGER='INCREASE_BURGER';
export const DECREASE_BURGER='DECREASE_BURGER';
Javascript
import {INCREASE_BURGER,DECREASE_BURGER} from './burgerActionTypes';
// Action functions which return action type and
// optional payLoad to burgerReducer
export const increaseBurgerAction=(parameter)=>{
return{
type:INCREASE_BURGER,
payload:parameter
}
}
export const decreaseBurgerAction=()=>{
return{
type:DECREASE_BURGER
}
}
Javascript
import {INCREASE_BURGER,DECREASE_BURGER} from './burgerActionTypes';
//initializing state
const initialState={
numberOfBurger:10
}
const burgerReducer=(state=initialState,action)=>{
switch(action.type){
case INCREASE_BURGER:return{
...state,
numberOfBurger:state.numberOfBurger+action.payload
}
case DECREASE_BURGER:return{
...state,
numberOfBurger:state.numberOfBurger-1
}
default:return state
}
}
export default burgerReducer;
Javascript
// Single file for exporting all actions
export {increaseBurgerAction} from './Burger/burgerAction';
export {decreaseBurgerAction} from './Burger/burgerAction';
Javascript
import {createStore} from 'redux';
import burgerReducer from './Burger/burgerReducer';
// Passing burgerReducer to createStore
const store=createStore(burgerReducer);
export default store;
Javascript
import React from 'react';
import {Provider} from 'react-redux';
import store from './MyAssets/Redux/store';
import Screen from './MyAssets/Screens/screen'
const App= () => {
return (
);
};
export default App;
Javascript
import React, { Component } from 'react'
import { Text, View,Button } from 'react-native'
import {connect} from 'react-redux'
import {increaseBurgerAction,decreaseBurgerAction} from '../Redux/index'
class Screen extends Component {
render() {
return (
Number Of Burger = {this.props.numberOfBurger}
)
}
}
const mapStateToProps=(state)=>{
return{
numberOfBurger:state.numberOfBurger
}
}
const mapDispatchToProps=(dispatch)=>{
return{
increaseBurger:(parameter)=>{dispatch(increaseBurgerAction(parameter))},
decreaseBurger:()=>{dispatch(decreaseBurgerAction())}
}
}
export default connect(mapStateToProps,mapDispatchToProps)(Screen);
Javascript
import {createStore,combineReducers} from 'redux';
import burgerReducer from './Burger/burgerReducer';
import pizzaReducer from './Pizza/pizzareducer';
// Combining burgerReducer and pizzaReducer in rootReducer
const rootReducer=combineReducers({
burgerReducer:burgerReducer,
pizzaReducer:pizzaReducer
})
// Passing rootReducer to createStore
const store=createStore(rootReducer);
export default store;
Javascript
import {PIZZA_DECREASE,PIZZA_INCREASE} from './pizzaActionsType';
// Initializing state
const initialState={
numberOfPizza:30
}
const pizzaReducer=(state=initialState,action)=>{
switch(action.type){
case PIZZA_INCREASE:return{
...state,
numberOfPizza:state.numberOfPizza+action.payload
}
case PIZZA_DECREASE:return{
...state,
numberOfPizza:state.numberOfPizza-1
}
default:return state
}
}
export default pizzaReducer;
import {PIZZA_INCREASE,PIZZA_DECREASE} from './pizzaActionsType';
// Action functions which return action type
// and optional payLoad to burgerReducer
export const increasePizzaAction=(parameter)=>{
return{
type:PIZZA_INCREASE,
payload:parameter
}
}
export const decreasePizzaAction=()=>{
return{
type:PIZZA_DECREASE
}
}
Javascript
// Single file for exporting all actions
export {increaseBurgerAction} from './Burger/burgerAction';
export {decreaseBurgerAction} from './Burger/burgerAction';
export {increasePizzaAction} from './Pizza/pizzaActions';
export {decreasePizzaAction} from './Pizza/pizzaActions';
Javascript
import React, { Component } from 'react'
import { Text, View,Button } from 'react-native'
import {connect} from 'react-redux'
import {increaseBurgerAction,decreaseBurgerAction,increasePizzaAction,decreasePizzaAction} from '../Redux/index'
class Screen extends Component {
render() {
return (
Number Of Burger = {this.props.numberOfBurger} Number Of Pizza = {this.props.numberOfPizza} {this.props.increasePizza(5)}}/>
{this.props.decreasePizza()}}/>
)
}
}
const mapStateToProps=(state)=>{
return{
numberOfBurger:state.burgerReducer.numberOfBurger,
numberOfPizza:state.pizzaReducer.numberOfPizza
}
}
const mapDispatchToProps=(dispatch)=>{
return{
increaseBurger:(parameter)=>{dispatch(increaseBurgerAction(parameter))},
decreaseBurger:()=>{dispatch(decreaseBurgerAction())},
increasePizza:(parameter)=>{dispatch(increasePizzaAction(parameter))},
decreasePizza:()=>{dispatch(decreasePizzaAction())}
}
}
export default connect(mapStateToProps,mapDispatchToProps)(Screen);
// Single file for exporting all actions
export {increaseBurgerAction} from './Burger/burgerAction';
export {decreaseBurgerAction} from './Burger/burgerAction';
import {PIZZA_INCREASE,PIZZA_DECREASE} from './pizzaActionsType';
// Action functions which return action type
// and optional payLoad to burgerReducer
export const increasePizzaAction=(parameter)=>{
return{
type:PIZZA_INCREASE,
payload:parameter
}
}
export const decreasePizzaAction=()=>{
return{
type:PIZZA_DECREASE
}
}
index.js:
Java脚本
// Single file for exporting all actions
export {increaseBurgerAction} from './Burger/burgerAction';
export {decreaseBurgerAction} from './Burger/burgerAction';
export {increasePizzaAction} from './Pizza/pizzaActions';
export {decreasePizzaAction} from './Pizza/pizzaActions';
screen.js –修改我们的屏幕组件代码以使用披萨动作和状态。
Java脚本
import React, { Component } from 'react'
import { Text, View,Button } from 'react-native'
import {connect} from 'react-redux'
import {increaseBurgerAction,decreaseBurgerAction,increasePizzaAction,decreasePizzaAction} from '../Redux/index'
class Screen extends Component {
render() {
return (
Number Of Burger = {this.props.numberOfBurger} {this.props.increaseBurger(5)}}/>
{this.props.decreaseBurger()}}/>
Number Of Pizza = {this.props.numberOfPizza} {this.props.increasePizza(5)}}/>
{this.props.decreasePizza()}}/>
)
}
}
const mapStateToProps=(state)=>{
return{
numberOfBurger:state.burgerReducer.numberOfBurger,
numberOfPizza:state.pizzaReducer.numberOfPizza
}
}
const mapDispatchToProps=(dispatch)=>{
return{
increaseBurger:(parameter)=>{dispatch(increaseBurgerAction(parameter))},
decreaseBurger:()=>{dispatch(decreaseBurgerAction())},
increasePizza:(parameter)=>{dispatch(increasePizzaAction(parameter))},
decreasePizza:()=>{dispatch(decreasePizzaAction())}
}
}
export default connect(mapStateToProps,mapDispatchToProps)(Screen);