如何通过JavaScript将React JSX组件附加到HTML
我创建了一个自定义烤面包组件,如下所示:
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用
Render()
函数时,删除了目标元素的所有内容,并加载了新内容。如果要将内容附加到现有的HTML,则可以创建一个新的&lt; div; gt;
(或任何其他元素)并附加元素。然后在新创建的元素中渲染内容。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.改变您的想法是正确的方法是将该组件添加到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.