React Redux-UseSelector更新两次通过道具的数组

时间:2020-08-27 19:29:32

标签: reactjs redux

我在更新通过道具收到的某些信息时遇到了一些麻烦。 我有一个带有空数组的父组件,它将模拟数据库。 我有一个窗体,它是一个子组件,它通过props接收此数组。 我使用useSelector来获取表单数据,然后将结果推送到数组,但是这个push方法被调用了两次。

这是注册成功后呈现的组件。

import React from "react";

import "./Result.css";
import { useSelector,} from "react-redux";
import Button from "../Button/Button";


export const Result = ({ clientsList }) => {
  const state = useSelector((state) => state);

  clientsList.push(state);
  console.log(clientsList);

  return (
    <div className="form-container">
      <h3>Successfuly registered</h3>      
      <Button destination="/" style="btb-primary" title="Voltar para a home" />
    </div>
  );
};

一次提交表单后,Console.log。


(2) […]
​
0: {…}
​​
address1: "Some address"
​​
address2: ""
​​
birthdate: "05/04/1953"
​​
cep: "12345"
​​
cpf: "123456789"
​​
email: "johndoe@gmail.com"
​​
income: "2000"
​​
lastName: "Doe"
​​
name: "John"
​​
phone: "5511456789"
​​
<prototype>: Object { … }
​
1: {…}
​​
address1: "Some address"
​​
address2: ""
​​
birthdate: "05/04/1953"
​​
cep: "12345"
​​
cpf: "123456789"
​​
email: "johndoe@gmail.com"
​​
income: "2000"
​​
lastName: "Doe"
​​
name: "John"
​​
phone: "5511456789"
​​
<prototype>: Object { … }
​
length: 2
​
<prototype>: Array []

任何人都知道如何使向阵列的推送仅发生一次?

0 个答案:

没有答案