GithubHelp home page GithubHelp logo

Comments (10)

matt-wigg avatar matt-wigg commented on April 28, 2024 2

This post offers a solution by use of the key prop - santidalmasso

This is an alternative code snippet:

// actions.js

export async function submitFormAction(state, formData) {
  try {
    // do something
    return {
      ...state,
      error: null,
      resetKey: Date.now().toString(), // Generate a new resetKey to trigger form reset
    };
  } catch (error) {
    console.error("Error during form submission:", error);
    return {
      ...state,
      error: "Submission failed. Please try again.",
    };
  }
}
// FormComponent.js

import { useFormState } from "react-dom";
import { submitFormAction } from "./actions";

export default function SimpleForm() {
  const [response, submitForm] = useFormState(submitFormAction, undefined);

  return (
    <div className="form-container">
      <form action={submitForm} key={response?.resetKey}>
        <input type="text" placeholder="Your Input" name="inputField" />
        <button type="submit">Submit</button>
        {response?.error && <p>{response.error}</p>}
      </form>
    </div>
  );
}

from react.

FleetAdmiralJakob avatar FleetAdmiralJakob commented on April 28, 2024 1

OK, thank you @Eyuelb. But this seems like a lot of code for this very simple task. I can't imagine what other people have to do for their use cases. I will try this one out as fast as I can and give feedback. But I hope that there will be an easier solution in the future.

EDIT: I worry a little bit about alle the anys

from react.

FleetAdmiralJakob avatar FleetAdmiralJakob commented on April 28, 2024

This feature is urgently needed as able to see in this discussion: vercel/next.js#58448

from react.

Eyuelb avatar Eyuelb commented on April 28, 2024

hi @FleetAdmiralJakob can you explain more and tell the input the output u need more like what u have tried and failed

from react.

FleetAdmiralJakob avatar FleetAdmiralJakob commented on April 28, 2024

@Eyuelb I tried to use useFormState but this was not resetting the form with JS Enabled.
I tried to use useRef, but then I can't react to different cases (success, error) and also you loose the progressive enhancement.

from react.

Eyuelb avatar Eyuelb commented on April 28, 2024

A quick fix is

export function processDataByType(data: any): any {
  if (typeof data === "string") {
    return "";
  } else if (typeof data === "boolean") {
    return data;
  } else if (Array.isArray(data)) {
    return [];
  } else {
    // You can handle other data types here if needed
    return null; // Default case, return null for other types
  }
}
Object.keys(formState.defaultValues)
.forEach((key:any) =>
 setValue(key,processDataByType(formState.defaultValues[key])
))

This basically resets the form

from react.

FleetAdmiralJakob avatar FleetAdmiralJakob commented on April 28, 2024

A quick fix is

`export function processDataByType(data: any): any {
if (typeof data === "string") {
return "";
} else if (typeof data === "boolean") {
return data;
} else if (Array.isArray(data)) {
return [];
} else {
// You can handle other data types here if needed
return null; // Default case, return null for other types
}
}

Object.keys(formState.defaultValues).forEach((key:any) => setValue(key,processDataByType(formState.defaultValues[key])))`

This basically resets the form

Could you pls format the code so it's easier to read.

from react.

Eyuelb avatar Eyuelb commented on April 28, 2024

@FleetAdmiralJakob did you find a solution

from react.

FleetAdmiralJakob avatar FleetAdmiralJakob commented on April 28, 2024

@Eyuelb Unfortunately not, I think React needs a general solution for this.

from react.

PedroMarianoAlmeida avatar PedroMarianoAlmeida commented on April 28, 2024

A quick fix is
export function processDataByType(data: any): any { if (typeof data === "string") { return ""; } else if (typeof data === "boolean") { return data; } else if (Array.isArray(data)) { return []; } else { // You can handle other data types here if needed return null; // Default case, return null for other types } } Object.keys(formState.defaultValues).forEach((key:any) => setValue(key,processDataByType(formState.defaultValues[key])))
This basically resets the form

Could you pls format the code so it's easier to read.

A quick fix is

export function processDataByType(data: any): any {
  if (typeof data === "string") {
    return "";
  } else if (typeof data === "boolean") {
    return data;
  } else if (Array.isArray(data)) {
    return [];
  } else {
    // You can handle other data types here if needed
    return null; // Default case, return null for other types
  }
}

Object.keys(formState.defaultValues).forEach((key: any) =>
  setValue(key, processDataByType(formState.defaultValues[key]))
);

This basically resets the form

from react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.