I have a React/TypeScript application that makes requests of a REST API.
One of the components in my app requests information that I would like to be able to use elsewhere, in other components.
Is it possible to make this API call in some other folder, and then import the result into my components where needed?
Or is it necessary to request this information each time it is needed?
I am quite new to React, so not sure what the intended way of doing this is
2
Answers
please explore Custom hooks and global state management.
as you said you are a beginner, I suggest you to explore for the above terms.
you will get the answer you need
Starting from what others said, the basic knowledge you need is about React context
This makes so that a parent component (called the
Context provider
) passes some data to all its children, which can be accessed by auseContext
hook. A powerful behavior is that children can modify too the context value, by using a setter function available from the context itself. I suggest you to check this question here and its accepted answer.