React-Redux 和 Connect - 为什么我的状态在点击时没有更新?

2023-12-15

我是 redux 的新手,正在编写一个简单的投票前端,允许用户对他们最喜欢的框架(Angular、React、Vue)进行投票。当用户点击他们想要投票的框架时,我打算将商店中的投票增加一票。

我在用着combineReducers and connect以促进这一点。然而,每次点击后,该州总是落后一票。投票一票后,状态为 0。点击 2 次后,投票状态为 1,依此类推。

这是我的index.js我在其中创建商店并渲染应用程序的文件

//index.js
const combinedReducers = combineReducers({
    "votes": votesReducer,
    "currentPercentages": currentPercentageReducer,
    "pastPercentages": pastPercentageReducer
});

let store = createStore(combinedReducers);

ReactDOM.render(
    <Provider store={store}>
        <App />
    </Provider>, 
    document.getElementById('root')
);

这是我的行动

export function voteAction(framework){
    return {
        type: "VOTE",
        payload: framework
    }
}

下面是更新投票状态的相关reducer

const initialVoteState = { angular: 0, react: 0, vue: 0 };

export function votesReducer(state=initialVoteState, action){
    switch(action.type){
        case "VOTE":
            if (action.payload === 'angular'){
                return Object.assign({}, state, {
                    angular: state.angular + 1
                });
            } else if (action.payload === 'react'){
                return Object.assign({}, state, {
                    react: state.react + 1
                });
            } else {
                return Object.assign({}, state, {
                    vue: state.vue + 1
                });
            }
        default:
            return state;
    }

}

最后,这是我拥有的组件,它具有单击处理程序以方便投票并相应地更新状态。

@connect(state => {
    return {
        votes: {
            angular: state.votes.angular,
            react: state.votes.react,
            vue: state.votes.vue
        },
        currentPercent: {
            angular: state.currentPercentages.angular,
            react: state.currentPercentages.react,
            vue: state.currentPercentages.vue
        },
        pastPercent: {
            angular: state.pastPercentages.angular,
            react: state.pastPercentages.react,
            vue: state.pastPercentages.vue
        }
    }
})
export default class InfoArea extends Component {
    constructor(props){
        super(props);
        this.votedAngular = this.votedAngular.bind(this);
        this.votedReact = this.votedReact.bind(this);
        this.votedVue = this.votedVue.bind(this);
    }

    votedAngular(){
        this.props.dispatch(voteAction('angular'));
        console.log(this.props.votes.angular);
        //the log above will output 0 after the first vote is cast, 1
        after the second vote is cast etc. Why is this the case? Should 
        it not be set to the correct value here since the action has 
        already been dispatched?

    }

    votedReact(){
        this.props.dispatch(voteAction('react'));
    }

    votedVue(){
        this.props.dispatch(voteAction('vue'));
    }

    render(){
        return (
        <div className="info-container">
            <img 
                style={{"marginTop":"25px"}} 
                className="app-img" 
                src={require("../../public/brainbulb.svg")}>
            </img>
            <h2 className="wide">What is your favourite front-end framework in 2017?</h2>
            <h4 style={{"marginTop": "0"}}>Click an image below to vote!</h4>
            <div className="row">
                <div className="images-container">
                    <img
                        onClick={this.votedAngular} 
                        className="framework-logo" 
                        src={require("../../public/angular.png")}>
                    </img>
                    <img 
                        onClick={this.votedReact}
                        className="framework-logo" 
                        src={require("../../public/react.png")}>
                    </img>
                    <img 
                        onClick={this.votedVue}
                        className="framework-logo"
                        src={require("../../public/vue.png")}
                    ></img>
                </div>
            </div>
        </div>
        );
    }
}

另外值得注意的是,当我投票并在浏览器控制台中查看我的商店状态时,它是准确的。然而,我的组成部分仍然落后一票。任何帮助将不胜感激。


您无法立即更新状态的原因是因为更新存储的操作是异步的并且写入

votedAngular(){
    this.props.dispatch(voteAction('angular'));
    console.log(this.props.votes.angular);
}

意味着您正在尝试在触发操作后立即检查该值。如果您想测试该值,我建议您在componentWillReceiveProps功能

componentWillReceiveProps(nextProps) {
   console.log(nextProps.votes.angular);
}

但是,对于需要测试 redux 值的此类操作,redux-devtools-extension是一种理想的工具。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React-Redux 和 Connect - 为什么我的状态在点击时没有更新? 的相关文章

随机推荐

  • 是否还可以自定义STL向量的“参考”类型?

    是否可以定制reference of a std vector 直到 C 11 似乎可以通过Allocator模板参数 但现在不再了吗 根据文档 http en cppreference com w cpp container vector
  • 如何确定 gfortran 正在矢量化什么

    我正在尝试编写一个大规模并行蒙特卡罗代码 其中一部分将导出到 Xeon phi 协处理器 为了确保我有效地使用协处理器 我想看看编译器 当前为 gfortran 能够对代码的哪些部分进行矢量化 我知道我可以使用 ifort commane
  • Android 10 MediaStore 文件权限

    我仅在 Android 10 中遇到图像权限问题 我声明该问题仅适用于 Android 10 事实上 Android 11 和 Android 9 及更早版本都启用了写入和读取权限 在清单中我有
  • 剥离可执行文件 (Windows)

    我听说 strip 是一个可以使可执行文件变小的程序 我尝试从我的编译器 针对 Python 打开它 但是当运行 strip 时 我只是在命令提示符中看到 strip 未被识别为命令或程序 错误 那么我在哪里可以获得 Windows 版 s
  • 如何让图像看起来好像站在平台上(如果它“降落”在平台上)

    所以我正在创建我的第一个 2d java 游戏 我想知道如何让玩家看起来好像站在一个平台上 如果它落在平台上 问题在于 在我的游戏中 NINJA 始终位于屏幕中央并且从不移动 但只有背景和平台移动 关于如何解决问题有什么想法吗 r back
  • 如何调整 JTextField 的大小? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心以获得指导 如何调整 JTextFie
  • ASP.NET:向不同的 Web 表单添加限制

    我目前正在寻求一些建议和帮助 以了解如何避免人们访问页面 除非他们 1 已登录 2 具有访问该页面的正确角色 到目前为止 我已经完成了登录页面 注册页面和其他一些页面 我还有一个链接到这些页面的数据库 用于存储用户及其各自的角色 当前在注册
  • Flutter - 如何切换flutter通道而不需要每次都下载flutter & dart sdk

    目前我正在尝试 flutter web 为此我需要在 flutter master 频道上工作 但是 然后我需要处理其他项目 在他们身上 我正在开发颤振稳定通道 但是 每次我使用命令 flutter channel stable 或 flu
  • 将组合框字符串值转换为 int

    我有一个关于转换类型的问题 我想将当前选定的组合框值字符串更改为 int 但出现错误 My code int Parse age SelectedItem ToString 对于这个问题我能做什么 好的 现在我们知道错误了 您可以在尝试解析
  • xpath 查找特定根下具有特定名称的所有属性

    为了找到所有具有名称的属性myAttr在文档中我可以这样做 myAttr但是如果我想指定根并仍然在文档中查找具有该名称的所有属性怎么办 就像是 root whatever or nothing myAttribute 这样怎么样 root
  • SQL Server 中按 x 排序,然后按 y 列排序

    考虑一个像这样的表 debit credit code 0 10 5 5 0 3 0 11 2 0 15 1 7 0 6 6 0 2 5 0 1 我需要生成这样的结果集 首先借记 然后按代码列排序 debit credit code 5 0
  • 如何在 Flutter 中更改主题?

    所以我在这里尝试获取当前主题 无论是浅色还是深色 所以我可以相应地改变小部件颜色 但是 它不起作用 我使用 if 语句来知道何时是黑暗模式 但它总是 False 这是代码 顺便说一句 它在深色和浅色主题之间切换 但是当我尝试获取当前主题时
  • Subversion E160004 X的根节点的前身是Y但应该是Z

    我继承了一个大型 Subversion 存储库 74010 修订版 并且我正在尝试执行转储 加载以将存储库升级到 1 8 版本 以利用节省空间的功能 在尝试这个过程之前我跑了svnadmin verify对有问题的存储库进行检查 以确保该存
  • 在 Google 商店中将多个 Chrome 扩展程序作为单个项目发布

    Chrome 扩展程序和 Chrome 应用程序具有我需要实现某些功能的 API 但我无法仅使用扩展程序或仅使用应用程序或使用本机代码来实现此目的 所以我制作了一个扩展程序和一个应用程序 并使它们通过消息相互通信 一切正常 但现在我必须发布
  • 将表单提交到操作 php 文件

    我有一个表单 当用户单击 提交 时 我需要运行一个 php 文件 下面是表单和 php 文件
  • Spirit X3,如何让属性类型匹配规则类型?

    对于 Spirit X3 解析器的开发 我想使用语义操作 脚注 1 对我来说 控制如何将属性存储到 STL 容器中非常重要 这个问题是关于如何控制解析器属性 attr ctx 与规则类型 val ctx 匹配 以便可以正确分配它 也许这个问
  • 如何构建电影数据库和用户选择?

    我想创建电影数据库 用户可以在其中标记他 她观看和喜欢的电影 class Movies ndb Model watched ndb UserProperty liked ndb UserProperty 那行得通吗 我使用谷歌帐户 以后我应
  • PySpark - RDD 中对象的时间重叠

    我的目标是根据时间重叠对对象进行分组 我的每个对象rdd包含一个start time and end time 我可能效率很低 但我计划做的是根据每个对象是否与任何其他对象有任何时间重叠来为每个对象分配一个重叠 id 我有时间重叠的逻辑 然
  • 为什么Spring Boot时找不到bean?

    我以更方便的方式重新配置了 DAO 通过使用 JpaRepository 而不是手动执行所有样板代码 但现在每次我启动 Spring 应用程序时都会出现以下错误 APPLICATION FAILED TO START Description
  • React-Redux 和 Connect - 为什么我的状态在点击时没有更新?

    我是 redux 的新手 正在编写一个简单的投票前端 允许用户对他们最喜欢的框架 Angular React Vue 进行投票 当用户点击他们想要投票的框架时 我打算将商店中的投票增加一票 我在用着combineReducers and c