UseForm ในตอบโต้คือ"ใครคลิกเบื้องหลังเรื่อง"[ที่ซ้ำกัน]

0

คำถาม

ฉันกำลังใช้ useForm ตะขอในตอบโต้และสำหรับเหตุผลบางอย่างตอนที่ผมป้อนค่าในรูปแบบแล้วคลิกที่ปุ่มส่งรัฐไม่ได้ใช้ค่าของฉันส่งแต่ต้องใช้ค่าของว่างเปล่า

งั้นตอนที่ฉันป้อนออกจากค่าในรูปแบบและส่งมัน,ทางรัฐต้องใช้ค่าของคนแรก submission.

มันดูเหมือนว่ามันเสมอไป"หนึ่งคลิกเบื้องหลังเรื่อง"(ดูที่คอนโซล.ปูมบันทึกที่ 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 คือการแสดง asynchronously.

ถ้าคุณต้องการเห็นของรายการของรัฐแล้วคุณจะต้องใช้ 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ý
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่