【问题标题】:Unable to update redux store - React Native无法更新 redux 存储 - React Native
【发布时间】:2021-05-06 03:20:11
【问题描述】:

我不熟悉将 redux 用于 React Native,并且正在用一个简单的案例对其进行测试。我已经能够成功连接到商店,并且可以看到使用 redux 调试器正确调度了操作,但是,商店没有在调试器中更新。我尝试了几种不同的实现,但没有任何效果。任何帮助将不胜感激!

组件:

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); 

行动:

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


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

减速器:

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;

【问题讨论】:

    标签: javascript reactjs react-native redux store


    【解决方案1】:

    您在此处共享的代码看起来是正确的。我唯一可以建议的是,如果您在调试器中看到操作通过,那么您的问题可能出在 simpleReducer 中的数据/有效负载或逻辑上。

    在这种情况下,您已将其正确剥离,所以我几乎认为这实际上不是您正在运行的代码,它可能是您的构建过程中的某些东西?

    【讨论】:

    • 我可以看到调试器中记录的操作。我可以查看构建过程的其他区域,看看是否有问题。
    • 我发现了问题 - 我忘记了减速器中 SET_TEXT 周围的花括号,这解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多