Skip to content
Advertisement

Passing props to dynamically rendered components in React

I have a page that is displaying several of my star components that each have their own name and a prop called starType I am generating several different of these stars with the following code

JavaScript

And this is the star component

JavaScript

At the moment I want the user to be able to click on each star and have it lead to a page. I have achieved that with react-router’s dynamic routing

JavaScript

the issue is I want the page that is generated from my generateSystem component to have the starType prop passed to it by the star component. I am aware of React’s one way data flow and I think that might be the issue. How can I pass prop data from an auto generated component to another auto generated component?

My full code is viewable here. The components I’m talking about are in the interstellar-view and systems folder.

Advertisement

Answer

since you are passing name through URL params so passing starType using query params is an easy option.

So URL would look like this www.example.com/123?starType=red-giant

In your star.jsx, make a modification like this

JavaScript

In your App.js, make a modification like this

JavaScript

(We do not need to render and pass props since we can use useParams in GenerateSystem.js)

In your GenerateSystem.js, make a modification like this

JavaScript

Refs:

https://reactrouter.com/web/api/Hooks/useparams

https://reactrouter.com/web/example/query-parameters

EDIT:

You can use Redux-store/Context-API to have a global store, so that name and starType can be stored globally and can be accessed in different components

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement