Comments (10)
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.
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 any
s
from react.
This feature is urgently needed as able to see in this discussion: vercel/next.js#58448
from react.
hi @FleetAdmiralJakob can you explain more and tell the input the output u need more like what u have tried and failed
from react.
@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.
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.
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.
@FleetAdmiralJakob did you find a solution
from react.
@Eyuelb Unfortunately not, I think React needs a general solution for this.
from react.
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 formCould 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)
- Bug: React server components give type error in react 19 HOT 2
- Bug: Eager bailout when calling the state setter function multiple times HOT 4
- Bug: Multiple renderings in the same dom without being overwritten, all renders are displayed HOT 2
- Bug: React const function getting previous version of state value when calling the function. HOT 1
- Bug: Manually added event listener always triggered HOT 1
- donde verlo
- [DevTools Bug]: Can’t import chrome trace into react devtools
- [DevTools Bug] Children cannot be added or removed during a reorder operation.
- Bug: SetState with same value, rerenders one more time HOT 2
- [DevTools Bug]: TypeError: ie.handlePostCommitFiberRoot is not a function HOT 3
- Hey really.org
- Bug: Invalid options to hydrateRoot can error with "This root received an early update"
- Bug: `Children.map` raises error when passed async server component within client component HOT 3
- Bug: mouse events are bubbled up too far with some pointer-events rules HOT 1
- Bug: Unsupported HTML character references HOT 6
- Bug: React not defined in TypeScript when importing from npm HOT 8
- After using forwardRef in a JSX component, you encounter an error when using it in a TSX component. HOT 1
- [DevTools Bug] Could not find node with id "464" in commit tree HOT 1
- ⚠Phishing Website Warning!!! There are phishing website links in the document. HOT 6
- not showing components in inspect its stuck
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.