I have a block of HTML content that is being pulled in via a graphql query. Within this text string, I have 'variables' in the form %variable%
. I need to replace the variables with dynamically generated content. The easiest way would be to have the variable replaced with the output from a React component. The reason for wanting to use a Component in this case, is because I need to get data from another source to replace the variable, and the data will change based on different inputs, so I can't just replace string for string.
If I do content.replace("%variable%,<Component />)
I get:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vel lobortis massa. Fusce ac enim ullamcorper, vulputate diam ut, consequat diam. Duis ante odio, suscipit a rhoncus eget, sollicitudin vel nibh. Proin ut urna sed diam dictum commodo sed nec felis. In hac habitasse platea dictumst. Vivamus varius nulla mi, [Object object] convallis lorem aliquam ac. Quisque congue nibh sit amet nisl ultrices gravida. Integer ac lobortis nibh. Donec interdum placerat nibh, eget pharetra tellus rutrum nec. In varius arcu eu luctus posuere. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aenean porttitor orci et eros auctor, at porta dui bibendum. Donec sed consequat felis.
So, %variable
is being replaced by [Object object]
- not what I was hoping...
I've looked at some other responses to similar queries, but can't really see a solution that would apply in this instance.
Is this possible?
replace
function returns string. That's why it prints[Object object]
. How to do this? Interesting question :) – Mosh Feu