2

How do I add a line break into my ModalBodContent react component?

<ModalMain
  ModalBodyContent={`New account created. Enrolment email sent. \n\n Name: ${firstName} ${lastName} \n email: ${email}`}
/>
5
  • <br>? \n? . Commented Mar 26, 2019 at 4:07
  • yes, the \n doesn't work and if I use <br /> it appears as text. Commented Mar 26, 2019 at 4:08
  • Then check ModalMain documentation about how they suggest to add new lines. Commented Mar 26, 2019 at 4:09
  • It's my component. Commented Mar 26, 2019 at 4:16
  • Why are you passing a string, if you want html elements to happen? Pass (the React equivalent of) an HTML element: let bcontent = <div>...</div>; ...; <ModalMain ModalBodyContent={bcontent}/> and done. Commented Mar 26, 2019 at 4:30

3 Answers 3

2

your code perfectly break string line but you need to use CSS style white-space: pre-wrap; in you HTML element where you render that string line.

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

Comments

1

2 ways:

You can use dangerouslySetInnerHTML inside of your <ModalMain/>

function render() {
  return <div dangerouslySetInnerHTML={{__html: `New account created. Enrolment email sent. \n\n Name: ${firstName} ${lastName} \n email: ${email}`}} />;
}

Or you can pass ModalBodyContent as an element, and render it accordingly:

<ModalMain
  ModalBodyContent={
    <div>
      New account created. Enrolment email sent. 
      <br/><br/>
      Name: {firstName} {lastName} 
      <br/>
      email: {email}
    </div>
  }
/>

Can also do this:

<ModalMain>
  <div>
    New account created. Enrolment email sent. 
    <br/><br/>
    Name: {firstName} {lastName} 
    <br/>
    email: {email}
  </div>
</ModalMain>

Comments

1

It depends on what you're expecting to be passed into ModalBodyContent.

If you use this prop as a string, well, there's pretty well nothing you can do. But if you use it as a React.ReactNode, then you can pass there not only a string, but also any JSX, including, for example, React.Fragment:

<ModalMain
  ModalBodyContent={(
    <React.Fragment>
      New account created. Enrolment email sent.
      <br/>
      <br/>
      {`Name: ${firstName} ${lastName}`}
      <br/>
      {`email: ${email}`}
    </React.Fragment>
/>

To use the component this way, internals of ModalMain should be something like this:

render() {
  return (
    // some complex jsx, possibly...
    {this.props.ModalContent}
    // anything else...;
  )
}

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.