Symfony twig reactJS

我想从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 }} />

最佳答案 枝条:

<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')
);
点赞