0

could you please tell me how to remove wrapper div in react js ?

here is my code https://codesandbox.io/s/festive-oskar-nbwjs?file=/src/App.js

current output

 <div id="root">
   <div class="App">
      <h1>Hello CodeSandbox</h1>
      <div>
         <section>ddddd</section>
      </div>
   </div>
</div>

Expected output

<div id="root">
       <div class="App">
          <h1>Hello CodeSandbox</h1>
             <section>ddddd</section>
       </div>
    </div>

how to remove this wrapper div. <div dangerouslySetInnerHTML={createMarkup()} />;

user944513
  • 12,247
  • 49
  • 168
  • 318
  • https://stackoverflow.com/questions/48236588/using-fragment-to-insert-html-rendered-on-the-back-end-via-dangerouslysetinnerht – JBallin Dec 24 '20 at 05:36

4 Answers4

1

I do want to disclaim any knowledge of your app's full context and could be looking at it wrong, but I want to offer a more fundamental solution. Why not simply do it like this?

import React from "react";
import "./styles.css";

export default function App() {
  const Elem = () => <section>ddddd</section>

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <Elem />
    </div>
  );
}

This would certainly remove that wrapper, but again, there may be something I don't know about the app.

codemonkey
  • 7,325
  • 5
  • 22
  • 36
0

use <React.Fragment><React.Fragment/> instead of div this will work

0

Make it simple like:

<div className="App">
    <h1>Hello CodeSandbox</h1>
    { createMarkup() }
</div>

And change the function:

function createMarkup() {
    return <section>ddddd</section>;
}
dhruw lalan
  • 791
  • 1
  • 11
  • 23
0

You'll be able to do this:

    import Parser from 'html-react-parser';
        <div>
            {Parser('<div>
                 <section>ddddd</section>
              </div>', {
                replace: (domNode) => {
                    if (domNode.name === 'div') {
                        return <section>ddddd</section>;
                    }
                }
            })}
        </div>
codemonkey
  • 7,325
  • 5
  • 22
  • 36
sedhal
  • 522
  • 4
  • 13