How to render a HTML comment in React?

前端 未结 4 1456
有刺的猬
有刺的猬 2020-12-09 15:27

Currently the render method can only return a single element/component. See: here

In the discussion under that ticket some suggest to wrap multiple elements returned

4条回答
  •  醉话见心
    2020-12-09 16:00

    Assuming you are on React 16.8+ you may use a small functional component which lets you provide a text property and render an html comment.

    import React, {useEffect, useRef} from 'react';
    
    const ReactComment = ( props ) => {
        const el = useRef();
        useEffect( () => {
            el.current.outerHTML = ``;
        }, [] );
        return (
            
    ); }; export default ReactComment;

    You may then use it like so

    
        
        
            
            
         
        
    
    

提交回复
热议问题