UseForm在作出反应是"点击背后"[重复]

0

的问题

我使用的useForm钩在作出反应,并由于某些原因当我输入值的形成,并点击"提交"按钮,国家不采取值我提交,但是需要价值观的空。

然后,当我进入一个第二套价值观的形式和提交时,国家需要的价值,第一提交。

它似乎总是"一个击落后"(见。登录在registerAccount功能)

我相联系的handleSubmit内与registerAccount功能,它应执行更新的国家保持的账户上的第一次点击。

我失去了一些东西?

import {useForm} from 'react-hook-form'

const AddAccount = ()=> {

    const {register, handleSubmit, formState: { errors }} = useForm()
    
    const [items, setItems] = useState([])

    const registerAccount = (data)=> {
          setItems([...items, data]) 
          console.log(items)
    }

return(

<div>    

    <div className="grid grid-cols-1 justify-items-center">
        <h1 className="text-black font-bold py-2 text-2xl" >Agrega una cuenta</h1>
        <form onSubmit={handleSubmit(registerAccount)}>
            <table className="table-fixed">
                <thead>
                    <th className="w-1/2 "></th>
                    <th className="w-1/2 "></th>
                </thead>
                <tbody>
                    <tr>
                        <td><label className="py-1">Alias de la cuenta: </label></td>
                        <td><input defaultValue="" name="accountAlias" placeholder="Alias" 
                        className= "my-3 border-solid border-2 border-gray-900 py-1 px-3" 
                        {...register("accountAlias", {required: "Introduce el nombre de la cuenta"})}/>
                        <p className="text-red-500 text-sm">{errors.accountAlias?.message}</p>
                            </td>
                            
                    </tr>
                    <tr>
                        <td><label className="py-1">Tipo de cuenta: </label></td>
                        
                        <td><select name="accountType" className= "my-3 border-solid border-2 border-gray-900 py-1 px-3"
                        {...register("accountType", {required: "Selecciona el tipo de cuenta"})}>
                            <option value="">Selecciona...</option>
                            <option value="cuenta de gastos">Cuenta de gastos</option>
                            <option value="ahorro">Ahorro</option>
                            <option value="tarjeta de crédito">Tarjeta de crédito</option>
                            <option value="inversión">Inversión</option>
                            </select>
                            <p className="text-red-500 text-sm">{errors.accountType?.message}</p>
                            </td>
                    </tr>
                    <tr>
                        
                    </tr>
                </tbody>
            </table>

            <button className="bg-blue-500 text-white py-2 px-6 px-2 mt-4">Agregar</button>
        </form>
    </div>

</div>
)
}

export default AddAccount```
1

最好的答案

0

问题是在你的控制台。日志(). 你写到控制台登录在您之前的状态得到更新。 由于useState是执行异步的。

如果你想要看看你的项目的状态,然后你需要使用useEffect如下。

   useEffect(() => {
        console.log(items)
    }, [items]);
2021-11-24 05:29:23

很好,谢谢!
Robtc83

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................