reactjs - Next.JSReact form, order of execution issue - Stack Overflow

While working on a Next.JS web app, I have an issue concerning the execution of some code.The relevant

While working on a Next.JS web app, I have an issue concerning the execution of some code.

The relevant TSX code follows:

"use client";
import {useState,ChangeEvent} from 'react';
import {useFormState} from "react-dom";

import {userLogin} from "...";

export interface LoginResponse {
  status: number;
}

const initialState: LoginResponse = {
  status: -1,
};


const Login = () => {
  const [formState, formAction] = useFormState(userLogin, initialState);
  const [inpMail, setInpMail] = useState('')
  const [logMail, setLogMail] = useState('')

    function mailUpdate(evt:ChangeEvent<HTMLInputElement>) {
    setInpMail(evt.target.value)
    } /* End of radioSelect */

  return(
    <div className="flex flex-col min-h-fit bg-slate-300 w-1/2 items-center" >
      .....
      <form className="p-4 flex flex-col w-80 bg-stone-300 items-center"
            action={formAction} method={"post"} >
        <input .... />
        <input .... />
        ....
        <button className="bg-cyan-500 text-xl rounded-lg w-1/3"
                onClick={() => {
                          setLogMail(inpMail)
                          // setInpMail('')
                        }}
                type="submit">Login</button>
      </form>
    </div>
  )
} /* End of Login */

.....

export default Login;

Finally comes my problem. When the button is clicked, both the onClick function and formAction gets executed. And what I want is to perform some house-cleaning work in the onClick after the formAction has done its things. But it seems to be that onClick is executed prior to formAction, which becomes a problem. Is there something I could do to control this order ?

While working on a Next.JS web app, I have an issue concerning the execution of some code.

The relevant TSX code follows:

"use client";
import {useState,ChangeEvent} from 'react';
import {useFormState} from "react-dom";

import {userLogin} from "...";

export interface LoginResponse {
  status: number;
}

const initialState: LoginResponse = {
  status: -1,
};


const Login = () => {
  const [formState, formAction] = useFormState(userLogin, initialState);
  const [inpMail, setInpMail] = useState('')
  const [logMail, setLogMail] = useState('')

    function mailUpdate(evt:ChangeEvent<HTMLInputElement>) {
    setInpMail(evt.target.value)
    } /* End of radioSelect */

  return(
    <div className="flex flex-col min-h-fit bg-slate-300 w-1/2 items-center" >
      .....
      <form className="p-4 flex flex-col w-80 bg-stone-300 items-center"
            action={formAction} method={"post"} >
        <input .... />
        <input .... />
        ....
        <button className="bg-cyan-500 text-xl rounded-lg w-1/3"
                onClick={() => {
                          setLogMail(inpMail)
                          // setInpMail('')
                        }}
                type="submit">Login</button>
      </form>
    </div>
  )
} /* End of Login */

.....

export default Login;

Finally comes my problem. When the button is clicked, both the onClick function and formAction gets executed. And what I want is to perform some house-cleaning work in the onClick after the formAction has done its things. But it seems to be that onClick is executed prior to formAction, which becomes a problem. Is there something I could do to control this order ?

Share Improve this question asked Nov 21, 2024 at 3:17 MichelMichel 11.8k20 gold badges101 silver badges217 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can control the whole process by removing the default behaviour for the form, and then programmatically submitting the form at the very end of the onClick()

    <form onSubmit={e => e.preventDefault()} 
          ref={ref => this.form = ref}
          className="p-4 flex flex-col w-80 bg-stone-300 items-center"
          action={formAction} method={"post"} >
            <input .... />
            <input .... />
            ....
            <button className="bg-cyan-500 text-xl rounded-lg w-1/3"
                    onClick={() => {
                              setLogMail(inpMail)
                              // setInpMail('')
                              this.form.dispatchEvent(
                               new Event("submit", { cancelable: true, bubbles: true })
                               )
                            }}
                    type="submit">Login</button>
          </form>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742315983a4420846.html

相关推荐

  • reactjs - Next.JSReact form, order of execution issue - Stack Overflow

    While working on a Next.JS web app, I have an issue concerning the execution of some code.The relevant

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信