Attaching React.js to a template rendered on the server with Twig

Attaching React.js to a template rendered on the server with Twig



X
by DNSUnlocker
<div class="item-content-left" data-reactid="862"><div
class="item-time-container" data-reactid="863"><time
class="item-time" title="su 12.11. 12:30" datetime=
"2017-11-12T10:30:12.000Z" data-reactid="864">

Using React 16 with Twig templates

import * as React from 'react';
import * as ReactDOM from 'react-dom';

class App extends React.Component {
  render() {
  return <table>
    <tbody>
    <tr>
      <th>Name</th>
      <th>Address</th>
      <th>Age</th>
    </tr>
    { initialState.employees.map( (employee) => {
      return <tr key={employee.uuid}>
      <td>{employee.name}</td>
      <td>{employee.address}</td>
      <td>{employee.age}</td>
      </tr>
    }) }
    </tbody>
  </table>
  }
}

ReactDOM.render(<App />, document.getElementById('employee-list'));
<h1>Employees</h1>
<div id="employee-list"></div>
<div id="employee-list">
<table>
  <tbody>
  <tr>
    <th>Name</th>
    <th>Address</th>
  </tr>
  {% for employee in employees %}
    <tr>
      <td>{{ employee.name }}</td>
      <td>{{ employee.address }}</td>
    </tr>
  {% endfor %}
  </tbody>
</table>
</div>
ReactDOM.hydrate(<App />, document.getElementById('employee-list'));
Warning: Did not expect server HTML to contain the text node "
    " in <div>. (warning.js:33)
<div id="employee-list">{% spaceless %}
<table>
    ...our complete template, loops and all...
</table>
{% endspaceless %}</div>
Warning: Expected server HTML to contain a matching <th> in <tr>.
<div id="employee-list">{% spaceless %}
<table>
  <tbody>
  <tr>
    <th>Name</th>
    <th>Address</th>
    <th>Age</th>
  </tr>
  {% for employee in employees %}
    <tr>
      <td>{{ employee.name }}</td>
      <td>{{ employee.address }}</td>
      <td>{{ employee.age }}</td> 
    </tr>
  {% endfor %}
  </tbody>
</table>
{% endspaceless %}</div>

Inline styles with JSX and Twig

Warning: Prop `style` did not match. Server: "padding:1em" Client: "background-color:grey;padding:1em"
Warning: Prop `className` did not match. Server: "heade" Client: "header"

Conclusion

Comments

Popular posts from this blog

A Tailwind CSS Preset for Laravel 5.5

Short and safe array iteration

PHPStorm's performance