Mit
Mit

Reputation: 349

Can we dynamically create links with React JS?

Imagine I wanted to create a web application with React like Instagram where users can add pictures. Every picture added is then associated to a single URL, like this one :

https://www.instagram.com/p/BTg4ZopggE9

How can we do that?

I know there is this module 'react-router' but as far as I know, it works only with a predefined set of routes that the programmer has hard-coded (The URLs can't be created dynamically).

Upvotes: 3

Views: 1222

Answers (1)

Chaim Friedman
Chaim Friedman

Reputation: 6253

While it is true that the routes in react-router are pre defined, the params can be dynamic. For instance, you can have a route like /images/:imageId, ImageId would be nothing more that a variable name to hold the image id. Now when a user would navigate there they would see the url like so, /images/12ert56 or something like that. Then the component to which they were routed to will read the image id out of the param by calling this.props.params.imageId and then the component will show them the corresponding image.

Upvotes: 3

Related Questions