I am having array of data which I am maping using .map method. How can I remove duplicate code i.e.
<a href={usp.url}>
<div>{usp.text}</div>
</a>
Code can be found on code sandbox https://codesandbox.io/s/modern-haze-b9016d?file=/src/App.tsx
I am having array of data which I am maping using .map method. How can I remove duplicate code i.e.
<a href={usp.url}>
<div>{usp.text}</div>
</a>
Code can be found on code sandbox https://codesandbox.io/s/modern-haze-b9016d?file=/src/App.tsx
2
Answers
Do you mean creating a component like this?
You can put your link into another component, e.g.:
MyLink
and export it from the file.MyLink.tsx :
Then in your
App.tsx
, replace<a>...</a>
with<MyLink/>
and add the required properties to it:You can use
Note: I see that you are using a map without
key
, you should always use unique keys when iterating over a list.