gpt4 book ai didi

Symfony twig + ReactJS

转载 作者:行者123 更新时间:2023-12-03 13:46:35 25 4
gpt4 key购买 nike

我想从twig模板渲染一个reactJS组件。是否可以?

我尝试过:

<div class="top-line">
<h1>{{ username }}</h1>

{% verbatim %}
<script type="text/html" id="follow-button">
<FriendshipButton user={{ app.user }} />
</script>
{% endverbatim %}
<div id="follow-button-mp"></div>
</div>

在 JS 端:

React.render(
React.createElement(document.getElementById('follow-button').innerHTML),
document.getElementById('follow-button-mp')
);

出现错误:

 Uncaught Error: Invariant Violation: Invalid tag: 
<FriendshipButton user={{ app.user }} />

最佳答案

Twig :

<script type="text/javascript">

// you might have to serialise user to JSON here if you
// want a more complete user object
var user = '{{ app.user.id }}';

</script>

Javascript 组件(使用 JSX)

React.render(

<FriendshipButton user={user} />,
document.getElementById('follow-button-mp')
);

关于Symfony twig + ReactJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32466667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com