Let's say I have a string in React "x {y} z" and I would like to dynamically replace {y} with a hyperlink with target = "ylink" Both y and ylink would be dynamic e.g. there would be an object with the text to replace (y) and the target of the hyperlink (link). Is it possible to do this?
3 Answers
yes it's possible you can do as shown below
const getLink = ({href, name}) => {
return(
<a href={href} >{name}</a>
)
}
x {getLink({href: "google.com", name:"y"})} z // your react string goes here

- 181
- 3
- 13
-
Thanks for the response but what about if I don't know what the string is: e.g. "terms and conditions are here" and "click here to view the terms and conditions" In both cases "here" should be the hyperlink. The solution should accommodate for both cases. So the component is passed a string, and maybe an object that has the part of the text that should be a hyperlink, and what the href should be – lstewart Mar 14 '22 at 01:12
Try creating a custom CustomLink component. Using functional component approach:
const CustomLink = (linkTarget, displayString) => <a href={linkTarget}>{displayString}</a>;
You can additionally add properties to the anchor tag such as e.g. rel, target props (see documentation: https://www.w3schools.com/tags/tag_a.asp). Finally, you can evaluate your requested string, but I rather recommend just use JSX if you want to display it in DOM:
const requestedJSX = <div><span>x {<CustomLink linkTarget='ylink' displayString='y'/>} z</span></div>;
Span is there for whitespace retention - feel free to not use it according to your needs. If you are working with single-page app using routes and different pages / views, a good approach is to choose react-router library and build link systems using the react-router-dom readily available Link component (just import it). I have named the custom anchor tag code above different from "Link" as not to mix with the react-router import name.
Example: note that react-router approach does not work unless you fully set up react-router!
import { Link } from "react-router-dom";
const requestedContent = <div>X <Link to='ylink'>Y</Link> Z</div>;

- 639
- 1
- 10
- 14
-
Thanks for the response but what about if I don't know what the string is: e.g. "terms and conditions are here" and "click here to view the terms and conditions" In both cases "here" should be the hyperlink. The solution should accommodate for both cases – lstewart Mar 14 '22 at 01:08
-
There is no single definitive answer for each problem around your question, but, I would suggest you to find an answer to this new question on SO. If you do not know the text beforehand, how do you know the hyperlink which should be used with the text? It is possible that if you know the hyperlink, then you also know the text beforehand. If you just need to find substring "here" and replace it with the same hyperlink, see this answer for example https://stackoverflow.com/a/62819322/2563593 – Erkka Mutanen Mar 15 '22 at 07:03
An example snippet to format a text
const formatText = (text, ...args) => {
const items = text.split('{}');
args.forEach(
(arg, index) => items.splice(items.length - args.length + index, 0, arg)
);
return items.join("");
};
console.log(
formatText("Hello {} welcome {} test {}", "one", "two", "three")
);

- 193
- 2
- 8