【问题标题】:How to listen redux state changes in react hooks?如何在反应钩子中监听 redux 状态变化?
【发布时间】:2021-07-13 20:44:30
【问题描述】:

我现在有多个用户可以编辑的表单和按钮,如果 redux 的状态发生变化,我想显示一个按钮保存。

现场演示:display button save when the state changes

这是我的 redux。

const initialState = {
  firstName: "Kunta ",
  lastName: "Kinte",
  age: 35,
  country: "Ghana",
  color: "#000"
};

const DetailsReducer = (state = initialState, action) => {
  const { name, value } = action;

  return { ...state, [name]: value };
};

export default DetailsReducer;

这是我的 js 代码,如果 redux 状态发生变化,则显示保存按钮

    import React, { useState, useEffect } from "react";
    import { useSelector, useDispatch } from "react-redux";
    
    const Settings = () => {
      const fields = useSelector((state) => state);
      const dispatch = useDispatch();
      const [saveBtn, setSaveBtn] = useState(false);
    
      useEffect(() => {
        setSaveBtn(true); // show save btn if there is changes in state
      }, []);
    
      console.log(fields.firstName);
    
      return (
        <div>
          <div>
            <h1>Edit </h1>
            First Name:{" "}
            <input
              name="firstname"
              value={fields.firstName}
              onChange={(e) =>
                dispatch({ name: "firstName", value: e.target.value, type: "" })
              }
            />
            {saveBtn === true && <button className="btn-save">save </button>}
          </div>
        </div>
      );
    };
    
    export default Settings;


  [1]: https://codesandbox.io/s/multiple-inputs-kkm6l?file=/src/Components/Settings.js:0-816

我需要做什么来解决这个问题?

【问题讨论】:

    标签: javascript reactjs input redux react-redux


    【解决方案1】:

    你试过了吗?

    const fields = useSelector((state) => state.WHATEVER_REDUCER);
    
    useEffect(() => {
        setSaveBtn(true); // show save btn if there is changes in state
    }, [fields]);
    

    【讨论】:

      【解决方案2】:

      你可以试试这样的:

      <input
          name="firstname"
          value={fields.firstName}
          onChange={(e) =>
              dispatch({ name: "firstName", value: e.target.value, type: "" }, setSaveBtn(true))
          }
      />
      

      同时删除:

      useEffect(() => {
          setSaveBtn(true); // show save btn if there is changes in state
      }, []);
      

      【讨论】:

        【解决方案3】:

        你可以这样做。去掉效果挂钩,将setSaveBtn移到输入onChange,点击保存后,设置setSaveBtn为false即可。

            import React, { useState, useEffect } from "react";
            import { useSelector, useDispatch } from "react-redux";
            
            const Settings = () => {
              const fields = useSelector((state) => state);
              const dispatch = useDispatch();
              const [saveBtn, setSaveBtn] = useState(false);
            
              console.log(fields.firstName);
            
              return (
                <div>
                  <div>
                    <h1>Edit </h1>
                    First Name:{" "}
                    <input
                      name="firstname"
                      value={fields.firstName}
                      onChange={(e) => {
                        dispatch({ name: "firstName", value: e.target.value, type: "" })
                        setSaveBtn(true)
                      }}
                    />
                    {saveBtn === true && 
                      <button
                       onClick={() => setSaveBtn(false)}
                      className="btn-save">save </button>}
                  </div>
                </div>
              );
            };
            
            export default Settings;
        

        【讨论】:

        • 所以我需要在 ech 输入上执行这个 setSvaeBtn 吗?不,这是错误的,我只想要一个函数来检测状态是否发生变化然后做一些事情
        猜你喜欢
        • 2019-09-17
        • 2011-01-31
        • 1970-01-01
        • 2022-01-19
        • 2014-12-11
        • 2017-05-31
        • 2015-05-23
        • 2022-07-27
        相关资源
        最近更新 更多