0

I can't get line breaks to work inside a string variable with React Native. In my database I have documents with a field called name. Name is a string. Name can contains specific line breaks. Data is fetched in my React Native component and rendered in a FlatList.

I've tried various combinations of:

  • \n
  • {'\n'}

None of them work, the line break is render as text

\\ In database document
name: "Some\ntitle"
\\ or
name: "Some{'\n'}title"
\\ In React Native (simplified)
<FlatList
    renderItem={
        <Text>{item.name}<\Text>
    }
>
<\FlatList>

It is rendered as:
Some\ntitle or Some{'\n'}title

Instead of:
Some
title

----------- SOLUTION -----------

// In database
name: "Some\ntitle"

// In React Native
{item.name.replace('\n', '\n')}

// Render
Some
title

3
  • Possible duplicate of How can I insert a line break into a <Text> component in React Native? Commented Mar 29, 2019 at 3:30
  • Nope, like mentionned in my question {'\n'} isn't working in this case Commented Mar 29, 2019 at 3:42
  • The question misses the part where you retrieve a string from a database. That's where the problem likely needs to be addressed. Commented Mar 29, 2019 at 6:55

8 Answers 8

4

That \n line feed appears literally means that it was escaped at some point, it is \\n in fact.

It can be {item.name.replace('\\n', '\n')}.

The actual problem is that it was escaped at all. This may affect other escape sequences and should be solved in the place where a string was escaped.

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

5 Comments

You did it! Who's the real player? Who's the real MVP ??
Yep, will do. Thanks for your time 🙏
I answered this 30 minutes prior in a comment below. stackoverflow.com/questions/55410052/…
@lawrencealan I don't check other answers thoroughly so I was unaware of the comment. I don't consider SO a competition; I just provided the solution and the explanation. Any way, the comment should be a part of your answer because this is the real problem here.
It's obviously a competition, there's a scoring system and leaderboards :) – my answer answered his original question, but he expanded the scope over time, to which I expanded my answer and awaited his response, instead of responding to my comment, he accepted your answer, which is the same. All good, just worth noting.
1

Use {"\n"} in place of \n:

Some{"\n"}text

or

<Text>{`Some\ntext`}</Text>

3 Comments

your approach is correct but how will you do this for a dynamic data? like in this question Some Text is coming from database. So it will be like item.name. how will you set item.name in a new line?
I've already tried this and it didn't work. The Text component is the renderItem of a FlatList component. name is a string stored in a mongodb collection. What Ankush Rishi said
Use a replace – <Text>{item.name.replace(/\\n/g,'\n')}</Text>
1

Try this solution:

   {item.name.split("\n").map((i,key) => {
      return <Text key={key}>{i}</Text>;
   })}

1 Comment

This is a nice solution!
1

Using regex, replace all the \\n to \n

const t = d.replace(/\\n/g, "\n");

Comments

0

You can try this way:

<Text>{`{item.name}`}</Text>

3 Comments

Thanks but it's unpractical: when the items of a list are documents from a database the content isn't hardcoded is the component, the line break has to be coded in the document then render in React Native
no, i thought of that but the notation inside the component don't change anything: <Component>{item.name}</Component> same result as <Component>{{item.name}}</Component>
Can you share your requirements more or share more code?
0

Can you try this solution: we tested it and i seems working

  var str = item.name;
  var result = str.split("\n"); 
  return result.map((i, key) => <Text key={key}>{i + "\n"}</Text>);

2 Comments

I just updated the code, and it's working for me. please check
I suppose it should works for me too (@estus solution seems better though) but I can't manage to integrate it with my FlatList
0

When I've stored document content in a database I've used JSON.stringify() to format the string before saving it and that will cause the string to include the raw \n in the string like what you have. To then render the string with the \n actually converted to new lines you should be able to simply pass that string into JSON.parse().

Comments

-2

Try styling the component with style={{ whiteSpace: "pre" }} to preserve the spaces

<Text style={{ whiteSpace: "pre" }}>{item.name}</Text>

1 Comment

whiteSpace isn't a React Native property

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.