Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use escape characters in JSX

Tags:

reactjs

jsx

I want to use some escape characters in my React application but I have not found any resource that how can I use escape character in my application.

Is any methodology available?

like image 814
aashir khan Avatar asked Jan 28 '18 08:01

aashir khan


People also ask

How do you escape special characters?

Special characters can serve different functions in the query syntax. To search for a special character that has a special function in the query syntax, you must escape the special character by adding a backslash before it, for example: To search for the string "where?", escape the question mark as follows: "where\?"

How do you show special characters in React?

Click the “Ω” command to show the special characters list, and then choose the character to be inserted in the editor.

What is ${} in JSX?

The ${} is template literals.


1 Answers

Use the same escape as Javascript (ECMAScript)

' single quote

" double quote

\ backslash

\n new line

\r carriage return

\t tab

\b backspace

\f form feed

For the HTML portion, use HTML escape characters.

There are some minor gotchas,to be aware of like evaluating escape chars between { }

HTML:

<div id="container">
    <!-- This element's contents will be replaced with MyComponent. -->
</div>

JSX:

class MyComponent extends React.Component {
  render() {
    console.info('Test line\nbreak');
    return <div>Hello {this.props.name} &lt;> </div>;
  }
}

ReactDOM.render(
  <MyComponent name="Stackoverflow  &lt; !-- comment in name -->" />,
  document.getElementById('container')
);

This program prints this to the console:

Test line
break

And the user's screen is the following:

Hello Stackoverflow < !-- comment in name --> <>

like image 177
rjdkolb Avatar answered Oct 19 '22 11:10

rjdkolb