1

I have a function which I need to return a string with line breaks, or divs that will render properly

export const showPeople = (peopleArray = []) => {
  let peopleString = peopleArray ? peopleArray.map((people) => {
    <div>
      `${people.name && people.name},
      ${people.last_name && people.last_name}`
    </div>
  }).join('') : ''
  return peopleString
}

I tried with divs, without divs, with return without return, and without join and i get [Object] for each person, in this particular case an empty string

I have seen similar questions but was not able to solve my problem

1

6 Answers 6

4

If the goal is to reduce a list of items to a single string that is rendered as a multi-line string, then you can use the white-space:pre-wrap; CSS rule to achieve this:

body {
  white-space: pre-wrap;
}

Once you've done that you should be able to render a multiple line string with line breaks on \n characters, as follows:

export const showPeople = (peopleArray = []) => {

  let peopleString = peopleArray ? peopleArray.map((people) => (
      `${people.name ? people.name : ''} ${people.last_name ? people.last_name : ''}`
  })
  .join('\n') : ''

  return peopleString
}

I've posted a working version here for you to see also

Hope this helps!

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

4 Comments

I want the line break between the different people, not between name and last name
@user3808307 ah I understand , just updated answer - does this help you?
@user3808307 ok , I've just posted an update - does this work for you?
No, I tried that, thanks. I've solved it by doing the map in the render now, None of the solutions work
3

You dont want to return a string you want to return an array of elements:

let peopleString = peopleArray.map((people) => (
  <div>
   {people.name || ""},
   {people.last_name || ""}
  </div>
));
return peopleString

Comments

3
export const showPeople = (peopleArray = []) => {
  let peopleString = peopleArray ? peopleArray.map(people => 
  `<div>
      ${people.name}, ${people.last_name}
    </div>`
  ).join('') : ''
  return peopleString
}

var peoples = [{name: 'Jhon', last_name: 'Doe'},{name: 'Mario', last_name: 'Peach'}]

var peopleString = peoples.map(people => 
  `<div>
    ${people.name}, ${people.last_name}
  </div>`
).join('')

console.log(peopleString)

document.getElementById('container').innerHTML = peopleString
<div id="container"></div>

2 Comments

Hi @Luis That doesn't work, I get weird characters instead of the <div>, it's inward and outward arrows something similar to this <- div -> but with longer lines
This answer ignores the fact that the OP uses React.
1

Since you're not doing string concatenation with variables. You need to remove Template Strings inside your divs.

2 Comments

${people.name && people.name} is string concatenation with variables.
I am also concatenating with a comma between name and last name
1

It appears to me that you are not returning anything in that map function:

(people) => { ...string here... }

I would suggest, simply:

(people) => ( ...string here... )

or:

(people) => { return ...string here... }


Another issue might be that what you seem to intend to return in the map function is NOT a string, and probably(?) should be; it's hard to say without seeing the function in context.

Comments

1

So if you specifically want to use the HTML Line Break element to separate the strings you can use this.

import { Fragment, ReactNode } from "react";

const separateWithLineBreaks = (strings: string[]): ReactNode => {
  return (
    <Fragment>
      {strings.map((str, index) => (
        <Fragment key={index}>
          {str}
          {index < strings.length - 1 && <br />}
        </Fragment>
      ))}
    </Fragment>
  );
};

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.