简体   繁体   English

如何使用next.js注入动态html元素页面?

[英]how can injection dynamic html element to page with next.js?

how can dynamic injection html element to page with next.js? 如何使用next.js动态注入html元素页面? that these elements Unknown type like(input, checkbox, img,...). 这些元素未知类型(输入,复选框,img,...)。 this element specified with api that return json type like this: 这个元素用api指定,返回json类型,如下所示:

[{
   "id":"rooms",
   "title":"Rooms",
   "order":1,
   "type":"string",
   "widget":"select",
   "data":[{
            "Id":18,
            "ParentId":null,
            "Title":"One",
            "Level":null,
            "Childrens":[]
           },
            {"Id":19,
            "ParentId":null,
            "Title":"Two",
            "Level":null,
            "Childrens":[]
           },
            {"Id":20,
            "ParentId":null,
            "Title":"Three",
            "Level":null,
            "Childrens":[]
           }]
},
{
   "id":"exchange",
   "title":"Exchange",
   "order":0,
   "type":"boolean",
   "widget":"checkbox",
   "data":[]
}]

my try is: 我的尝试是:

Index.getInitialProps = async function({req, query}) {

    const res= await fetch('url api')
    var elements= await res.json() 

    var test = () => (
        <div>
            {...... convert json to html elements.......}
        </div>
    )

    return {
        test
    }
})
function Index(props) {
   return(
      <a>
        {props.test}
      </a>
   )
}

result is null, mean nothing for presentation. 结果为null,对于表示没有任何意义。

the question is, Do I do the right thing? 问题是,我做对了吗? Is there a better way? 有没有更好的办法?

What happens is that during the transfer of props from server to client in getInitialprops , JSON is serialized and so functions are not really serialized. 发生的事情是,在getInitialprops从服务器到客户端传递道具期间,JSON被序列化,因此函数不是真正序列化的。 See https://github.com/zeit/next.js/issues/3536 请参阅https://github.com/zeit/next.js/issues/3536

Your best bet is to convert the test data into a string of HTML data and inject it using dangerouslySetInnerHTML . 最好的办法是将测试数据转换为HTML数据字符串并使用dangerouslySetInnerHTML注入。 An example will be: 一个例子是:

class TestComponent extends React.Component {
    static async getInitialProps() {
        const text = '<div class="homepsage">This is the homepage data</div>';
        return { text };
    }

    render() {
        return (
            <div>
                <div className="text-container" dangerouslySetInnerHTML={{ __html: this.props.text }} />
                <h1>Hello world</div>
            </div>
        );
    }
}

The catch with this is that the string you return must be a valid HTML (not JSX). 这样做的结果是你返回的字符串必须是有效的HTML(而不是JSX)。 So notice I used class instead of className 所以请注意我使用了class而不是className

You can read more about it here: https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml 你可以在这里阅读更多相关信息: https//reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM