0

Say I have a string const s = "I love $FB", and I wanna get:

<p>I love <a href="/symbol/FB">$FB</a></p> by <RenderContent content={s}>

I can use reg to extract the symbol, but how can I turn the string into a React component, without using dangerouslySetInnerHTML?

1

3 Answers 3

1

An alternative render function for a component to use regex capturing groups, no validation is done on the .exec result however:

render() {
  const subs = /(^.*)(\$(.*))/.exec(this.props.content);
  return (
    <p>
      { subs[1] }
      <a href={`/symbol/${subs[3]}`}>{subs[2]}</a>
    </p>
  );
}
Sign up to request clarification or add additional context in comments.

Comments

1

In your render method you can do this:

<p>{this.props.s.split('$')[0]} 

 <a href="/symbol/{this.props.s.split('$')[1]}">${this.props.s.split('$')[1]}
 </a>
</p>

Comments

0

Use like this

ReactDOM.render(<RenderContent><p>I love <a href="/symbol/FB">$FB</a></p></RenderContent>, element);

Now in your RenderContent Component use the following code:

render(){
   return <div>{this.props.children}</div> //this will render your inner react component

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.