简体   繁体   English

Reactjs Redux 使用输入更新状态/存储中的数据值

[英]Reactjs Redux updating data values from the state/store with an input

I have a state that has multiple users initial State of the reducer我有一个具有多个用户初始状态的减速器状态

const initialState = [
  {
    id: 1,
   name:"thename",
   lastName: "Lastname"
  },
  {
    id: 2,
   name:"thename",
   lastName: "Lastname"
  },
  {
    id: 3,
   name:"thename",
   lastName: "Lastname"
  },
];

And a component that shows them in a table, and the table has the functionality to update this values.以及一个在表格中显示它们的组件,表格具有更新这些值的功能。 So the thing i want to do is to take this values, make a copy of them to the state of the component and update them with my inputs.所以我想要做的就是获取这些值,将它们复制到组件的状态并用我的输入更新它们。

Copying:复制:

const users= useSelector((state) => state.usersReducer);
const [userState, setUserState] = useState(users)
  useEffect(() => {
    setUserState(users); // if the users state from the redux grows
  }, [applicants]);

Mapping to display the data:映射以显示数据:

{ usersState.map( user=>{
<input onChange={(e) => handleChange(user)} value={user.name} >
})}

and with this handleChange并使用此 handleChange

const handleChange = (data) => {
    setUserState((user) => {
      return [...user.filter((x) => x.id !== data.id), data];
    });
  };

but when I type in the input nothing happen... Because I could have multiple users, i'm mapping through them, I did forms before while learning, and never had this problem where the input doesn't change at all.但是当我输入时什么也没有发生......因为我可以有多个用户,我正在通过他们进行映射,我之前在学习时做过表格,从来没有遇到过输入根本不改变的问题。 But never had to do something like this, so i'm in trouble.但从来没有做过这样的事情,所以我遇到了麻烦。

如果您还想更新 redux 状态,则需要调用dispatch通过handleChange函数中的 reducer 来更新 redux 状态。

I have noticed two issues here in your snippet.我在您的代码段中注意到了两个问题。

  • This component shouldn't have users as component state.此组件不应将用户作为组件状态。

The selector is approximately equivalent to the mapStateToProps argument to connect conceptually.选择器大约相当于 mapStateToProps 参数以在概念上进行连接。 The selector will be called with the entire Redux store state as its only argument.选择器将使用整个 Redux 存储状态作为其唯一参数被调用。 The selector will be run whenever the function component renders (unless its reference hasn't changed since a previous render of the component so that a cached result can be returned by the hook without re-running the selector).选择器将在函数组件渲染时运行(除非它的引用自组件的上次渲染以来没有改变,以便钩子可以返回缓存的结果而无需重新运行选择器)。 useSelector() will also subscribe to the Redux store, and run your selector whenever an action is dispatched. useSelector() 还将订阅 Redux 存储,并在调度操作时运行您的选择器。

  • dispatch should be call in handleChange method to update redux state and re-render component dispatch应该在handleChange方法中调用以更新 redux 状态并重新渲染组件

For example:例如:

  const dispatch = useDispatch()

  const handleChange = (e, user) => {
    dispatch({
      type: 'UPDATE_USER',
      data: {
        ...user,
        name: e.target.value
      },
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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