简体   繁体   中英

Unable to update redux store - React Native

I am new to using redux for React Native and am testing it with a simple case. I have been able to successfully connect to the store, and I can see the action is dispatched properly using the redux debugger, however, the store is not updating in the debugger. I've tried several different implementations, but nothing is working. Any help would be appreciated!

Component:

import React, { PureComponent } from 'react'
import { Text, TouchableOpacity, SafeAreaView, Alert, Button } from 'react-native'
import { Navigation } from 'react-native-navigation';
import { connect } from 'react-redux'
import simpleAction from '../store/actions/simpleAction'

class App2 extends PureComponent {
    constructor(props){
        super(props);
    }
    pressRedux = () => {
        const data = 'hello'
        this.props.simpleAction(data)
    }
    render() {
        return (
            <SafeAreaView>
                <Text>
                    {this.props.state.simpleReducer.text}
                </Text>
                <Button onPress = {this.pressRedux} title = 'Redux' />
            </SafeAreaView>
        )
    }
}


function mapStateToProps(state) {
    return {
      state: state
    };
  }

const mapDispatchToProps = {
    simpleAction
}

export default connect(mapStateToProps, mapDispatchToProps)(App2); 

Action:

import {SET_TEXT} from '../types/types'


export default function simpleAction(data) {
    return({
        type: SET_TEXT,
        payload: data
    })
}

reducer:

import SET_TEXT from '../types/types'

const INITIAL_STATE = {
    text: 'Hi'
}

const simpleReducer = (state = INITIAL_STATE, action ) => {
    switch(action.type){
        case SET_TEXT:
            return { ...state, text: action.payload};
        default:
            return state;
    }
}

export default simpleReducer;

The code you've shared here looks correct. Only thing I can suggest is, if you're seeing the action come through in the debugger, your issue is either with the data/payload or logic within simpleReducer.

In this case you have it properly stripped down so I'd almost think this isn't actually the code you are running, it might be something in your build process?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM