I have a react component A that is used to upload files, basically when rendered this component receives a file and uploads it, it also displays the file name and upload progress.
I would like to display the file name and progress also in a different part of the page.
If I try rendering the component A in different parts of the page it will upload the file twice.
How can I render A and then clone it’s DOM to another part of the page?
I don’t want to move A logic to parent component
2
Answers
If you want to display the file name and upload progress in a different part of the page without duplicating the file upload logic, you can use React’s Context API or state management libraries like Redux.
An example using React Context:
This can be done using very easily using React Context, or even with the
useState
hook. With context you can achieve your goal of not moving logic to a parent component per say, but logic will be added in the form of a context provider.