Navigate back to the homepage

how to handle apostrophe in react js?

Infinitbility
React
August 9th, 2022 · 1 min read
how to handle apostrophe in react js?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To handle apostrophe ( single quote, or double quote ) in react js, use the ( \ ) Escape characters or ( ` ) Template literals it will handle both types quotes single and double.

Let’s see how Escape characters and Template literals will solve the issue.

1let brokenString = 'I'm a broken string';
2
3console.log(brokenString);
4
5// Output
6// unknown: Unexpected token (1:24)
7
8brokenString = 'I\'m a broken string';
9
10console.log(brokenString);
11
12// Output
13// "I'm a broken string"
14
15brokenString = `I'm a "broken" string`;
16
17console.log(brokenString);
18
19// Output
20// `I'm a "broken" string`

Today, I’m going to show you How do I handle apostrophe in react js, as above mentioned, I’m going to use the above-mentioned ( \ ) Escape characters or ( ` ) Template literals.

Let’s start today’s tutorial how do you handle apostrophe in react js?

React js handle apostrophe example using Escape characters

Here, we will take sample string with quote and will use Escape characters to handle quote in string.

1import React, { useState, useEffect } from "react";
2export default function App() {
3 const [str, setStr] = useState("");
4
5 useEffect(() => {
6 setStr('I\'m a \"broken\" string');
7 }, []);
8
9 return (
10 <div className="App">
11 <p>{str}</p>
12 </div>
13 );
14}

Output

React js handle apostrophe example using Template literals

Here, we will take sample string with quote and will use Template literals to handle quote in string.

1import React, { useState, useEffect } from "react";
2export default function App() {
3 const [str, setStr] = useState("");
4
5 useEffect(() => {
6 setStr(`I'm a "broken" string`);
7 }, []);
8
9 return (
10 <div className="App">
11 <p>{str}</p>
12 </div>
13 );
14}

Output

In the above example, I have put the example to use escape characters and template literals in react js.

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to handle apostrophe in react native?

how to handle apostrophe in react native?

An example of handle apostrophe in react native with the step-by-step guide.

August 9th, 2022 · 1 min read
how to handle apostrophe in typescript?

how to handle apostrophe in typescript?

An example of handle apostrophe in typescript with the step-by-step guide.

August 9th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]