New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot upload to multiple FilePond instances with controlled state (state is cleared after attempting second upload) #153
Comments
It looks like
so I'm not sure this is related to FilePond |
@rikschennink I just updated the sandbox with a It is possible it's a problem not with FilePond but just my understanding of it. Is it possible to have multiple controlled FilePond components on the same page, and if so, how? That's my goal, but I've been going at it for a full day now and I haven't figured it out. |
Yes that should be possible. I don't know why the formData is being reset to undefined, I don't have enough experience with hooks. |
Okay, glad to hear it should be possible. I've decided to use something else in our project since this doesn't seem to be possible at the moment, but I hope you guys figure it out, it is a beautiful component. Thanks for taking a look. :) |
This https://codesandbox.io/s/react-filepond-forked-skdbc is the repaired version, it can work normally. |
The reason is that for Please refer to: |
@sweetliquid Oh wow, thank you so much!! Now I've got it. Thank you for the help <3 |
I'm having a similar issue where I have a multistep Formik form using react-albus for each step and i've created a custom class the holds with all my configurations so I can reuse it and then a component I use inside Formik. I 3 times in my Formik form and each has a different name and the name of the file state in is the name passed in the settings Reusable Filepond:
The Form Component:
The Use:
|
@Rafcin same here... all of my FilePond's are wrapped in a Formik component. I can add posts, edit posts, and remove posts, with my component, only if I make sure that I add the images as the very last step... as soon as I edit any other input field inside the Formik component, Filepond drops it's images, and/or resets to the initial image state. (If it had images already i.e. editing). This has been a concern of mine for almost 2 years now. No solution. I've tried everything. onupdatefiles appears to bork the Formik wrapper so I removed it altogether, because finding a way for them to sync with each other appears impossible.
If I load up a filepond component and feed it multiple initial files... i.e. for example 3. Hit the top left X on one file inside the component, it instantly removes it then puts it back, because it's running it's own management of files and can't be taken control of... as soon as you add 'onupdatefiles' to the Filepond component, it becomes uncontrolled and ignores the control, so if you control it, it ignores 'onupdatefiles', and becomes controlled. It's an unusual situation. But to add to this, my component refuses to work unless onupdatesfiles exists... but does nothing. i.e. Wish there was an acceptable working flow for this. |
Hey folks! Sorry to chime in on a very old, stale thread. That said, I've shared similar struggles recently and came across this as the closest example of what I was experiencing trying to wire up Ultimately, the pattern I was looking to achieve was to get the filepond react wrapper to be uncontrolled with a default value. From what I can see @rikschennink thought through this with a little trick to turn the "controlled" pattern on via the presence of the Lines 86 to 88 in 3af941f
However, this "default value" isn't necessarily available on first render in my case. (My form needs to be "resumable" — and therefore needs to rehydrate any user data that has been populated prior.) Ultimately, I achieved the desired result by manually manipulating the component instance variable const MyFormikFileInput = ({ name, multiple }) => {
const [field, meta, helpers] = useField<string | string[]>({ name, multiple });
const filepond = useRef<Nullable<FilePondInput>>(null);
const setRef = (instance: Nullable<FilePondInput>) => {
filepond.current = instance;
/**
* Ensure that after the initial value is present, we "convert" filepond to uncontrolled.
* This ensures that the "files" prop is treated as though it were `defaultValue`.
*/
if (filepond.current && meta.initialValue) {
filepond.current.allowFilesSync = false;
}
};
/** Provided all updated files update field in form state. */
const setFiles = (files: Optional<FilePondFile[]>) => {
const serverIds = files?.map(({ serverId }) => serverId).filter(Boolean);
helpers.setValue(multiple ? serverIds : serverIds?.[0]);
helpers.setTouched(true);
};
// Interpret any initial value as hydrated server ids to uploaded files
const initialFiles: Optional<FilePondInitialFile[]> = initialValue?.length
? ([] as string[]).concat(initialValue ?? []).map((fileId) => ({
source: fileId,
options: {
type: 'local',
},
}))
: undefined;
return (
<FilePondInput
name={field.name}
allowMultiple={multiple}
files={initialFiles}
onprocessfiles={() => setFiles(filepond.current?.getFiles())}
onupdatefiles={setFiles}
ref={setRef}
{...otherConfig}
/>
);
}; As I guess I'll cross that bridge when/if we get there, but I hope this proves helpful in terms of future consideration or current mitigation! 😄 |
@braxeatssnacks thanks for the detailed explanation on how to work around these limitations :) It is indeed a workaround to make FilePond "work" with external state. For version 5 of FilePond I'm regularly testing with various JS Frameworks to make sure it "just" works. |
I can't remember what my solution was for this, but I have since switched to Svelte and SvelteKit which seem to work a lot stabler with state. |
@michaelcuneo ❤️ Svelte, I used it to built Pintura and Hotlist and it's gonna power the FilePond v5 view as well. |
Nice! @rikschennink I switched over to Svelte about 2 years ago and haven't looked back. Our entire public facing laboratory with Administrative backend including 'Filepond' for the images, is all SvelteKit. Went from 18,000 lines of React to 1800 lines of Svelte, and 100/100 for all Chrome Lighthouse tests, (in the next version). It's bloody good how you can pure library now with Svelte and make a component like FilePond. |
@michaelcuneo Olala, love those abstract video headers 🔥 |
Describe the bug
Uploading to multiple FilePond components with controlled state fails; the state for all FilePond components is cleared once you try to upload to more than one component.
Logs
There are no errors in the browser console or dev server logs.
To Reproduce
See this sandbox: https://codesandbox.io/s/react-filepond-forked-cvck1?file=/src/App.js
Expected behavior
I expect both FilePond components to keep the uploaded file, and to update the controlled state correctly.
Information about your project:
Additional context
I'm not certain that this is a glitch with FilePond. It's possible that this is a logic error on my end. However, I'm quite stumped as to what is the correct way to have multiple controlled FilePond components. The code I have seems to control state correctly with a single FilePond component, and seems to also work with non-FilePond components (other form fields like text, input, etc) - which led me to believe this could be a FilePond issue, but I'm not sure. I would appreciate any enlightenment on this issue; if I'm doing it improperly, could you please let me know what is the proper way? And thank you for your work on this great library :)
The text was updated successfully, but these errors were encountered: