【问题标题】:How to reset values of react-redux?如何重置 react-redux 的值?
【发布时间】:2021-08-28 15:13:45
【问题描述】:

我一直在使用 MERN 堆栈。
我想知道如何重置 redux 值中的值。
如果我向后端/login 路由发送不正确的数据,我得到'Invalid email or password' 并将其保存为redux。我在前端看到了。更改路线时,在我将路线更改为登录(反应)页面后,我看到此消息。如果我刷新页面 redux 重置。
我有一个问题,如何不刷新更改路线重置值?

【问题讨论】:

  • 在加载登录组件时设置初始状态?
  • 获取你想要的数据到redux(初始数据)

标签: javascript reactjs redux react-redux react-router-dom


【解决方案1】:
import {reset} from 'redux-form';

...

dispatch(reset('myForm'));  // requires form name

【讨论】:

    【解决方案2】:

    我从您的问题中得到的是,当您从 page a 切换到 时,您想清除 redux 商店的 '无效的电子邮件或密码' b页

    这就是你可以做的,你可以通过创建和操作重置为''redux 中的数据,并在 page a 内卸载时调度它。

    page a

    里面有类似的东西
    componentWillUnmount() {
      dispatch(resetErrorMessageAction())
    }
    

    内部 actions.js

    function resetErrorMessageAction(data) {
      return {
        type: 'RESET_ERROR_MESSAGE'
      }
    }
    

    在你的减速器内部

    function(state, action) {
      switch(action.type) {
        case 'RESET_ERROR_MESSAGE':
          return {
            ...state,
            errorMessage: ''
          }
      }
    }
    

    【讨论】:

    • 我就是这么做的。但是我写了很多代码。我只想在更改路由重置值时在某处添加一些代码
    猜你喜欢
    • 2016-11-25
    • 2018-06-18
    • 2018-11-11
    • 2022-01-11
    • 2021-06-13
    • 1970-01-01
    • 2020-10-22
    • 2017-07-31
    • 1970-01-01
    相关资源
    最近更新 更多