如何通过JavaScript将React JSX组件附加到HTML

发布于 2025-02-12 03:47:13 字数 671 浏览 2 评论 0原文

我创建了一个自定义烤面包组件,如下所示:

<Snackbar
    open={open}
    autoHideDuration={6000}
    onClose={handleClose}
    message="Note archived"
    action={action}
  />

我不想将其连接到我在React应用中创建的每个页面上。相反,我想创建一个可以用来调用它的静态函数。类似的事情:

Swal.fire(
  'Good job!',
  'You clicked the button!',
  'success'
)

但是要使这种工作,我必须将组件附加到HTML。请我如何实现。我尝试执行此操作:

    ReactDOM.render(<OvToast
    content="hello"
    handleClose={() => console.log('this is it')}
    open
    title="success"
    type="success"
  />, document.querySelector('#root'));

但是它除了吐司以外,它删除了页面的所有内容。 请如何将JSX附加到HTML?我想获得吐司信息。

I created a custom toast component as seen below:

<Snackbar
    open={open}
    autoHideDuration={6000}
    onClose={handleClose}
    message="Note archived"
    action={action}
  />

I don't want to attach it to every page I create in my react app. Instead, I want to create a static function that I can use to call it. Something like this:

Swal.fire(
  'Good job!',
  'You clicked the button!',
  'success'
)

But for this to work, I have to append a component to the HTML. Please how can I achieve this. I tried doing this:

    ReactDOM.render(<OvToast
    content="hello"
    handleClose={() => console.log('this is it')}
    open
    title="success"
    type="success"
  />, document.querySelector('#root'));

but it removes all the content of the page except the toast.
Please how do I append Jsx to a HTML? I want to achieve a toast message.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欢烬 2025-02-19 03:47:13

当您使用Render()函数时,删除了目标元素的所有内容,并加载了新内容。如果要将内容附加到现有的HTML,则可以创建一个新的&lt; div; gt;(或任何其他元素)并附加元素。然后在新创建的元素中渲染内容。

function App() {
  return <React.Fragment>hello friend</React.Fragment>;
}

const div = document.createElement("div");
document.querySelector("#root").append(div);

const root = ReactDOM.createRoot(div);
root.render(<App />);

// Or if you are using React 17 or lower:
//
//     ReactDOM.render(<App />, div);
//
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<div id="root">
  <p>existing content</p>
</div>

When you use the render() function all the contents of the target element is removed and the new content is loaded. If you want to append the content to existing HTML you can create a new <div> (or any other element) and append the element. Then render the contents in the newly created element.

function App() {
  return <React.Fragment>hello friend</React.Fragment>;
}

const div = document.createElement("div");
document.querySelector("#root").append(div);

const root = ReactDOM.createRoot(div);
root.render(<App />);

// Or if you are using React 17 or lower:
//
//     ReactDOM.render(<App />, div);
//
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<div id="root">
  <p>existing content</p>
</div>

水溶 2025-02-19 03:47:13

改变您的想法是正确的方法是将该组件添加到app.js。

然后,使用redux,设置消息的值和布尔值等...以显示或隐藏组件。

现在,您可以使用Redux上的任何页面上更改消息的价值和iSshow的价值。

change your mind The correct way is to add that component to app.js.

Then, using redux, set the value of message and boolean etc... to show or hide the component .

Now you can change the value of message and isShow on any page you want using redux.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文