2

I'm ReactJS noob, so I am having this terrible problem: got variable named adminLink containing the following:

<span class="link" onclick="showLogin">Login</span>

I need to print it out, as HTML. If I do following

<div className="adminLink">
    {adminLink}
</div>

I get the text, not

2 Answers 2

7

You can also use dangerouslySetInnerHtml:

<div className="adminLink" dangerouslySetInnerHTML={{__html: adminLink}}>
    {adminLink}
</div>

It is well explained on the JSX wiki page about HTML entities why this is dangerous and what other choices you have.

Sign up to request clarification or add additional context in comments.

Comments

5

Hello you would simply need to do this:

myComp = React.createClass({
  render:function(){
    adminLink = [];

    adminLink.push(<span className="link" onclick="showLogin">Login</span>);

    render (<div className="adminLink">
              {adminLink}
            </div> )
  }
});

Try it. hope it helped.

EDIT:

please notice the "class" needs to be changed to className. Also: using an array is not neccessary, I just like to do it in case I have to attach other JSX later on

3 Comments

also onclick needs to be onClick, and you need to pass a function to it (a string is not a function)
Also you dont need to push it to an array. You could also just add it to a variable like this var adminLink = (<span className="link" onclick="showLogin">Login</span>);
@magnudae just as I mentioned in my EDIT :) Its just my style of adding JSX and optionally pushing more in some later point

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.