Giter VIP home page Giter VIP logo

react-render's Introduction

React Render

Render React components on the server side in Django, also called "isomorphic React". You would do this for faster page loads, to make it friendlier to web crawlers and for SEO.

Example

from react_render.django.render import render_component

props = {
    'foo': 'bar',
    'woz': [1,2,3],
}

rendered = render_component('path/to/component.jsx', props=props)

print(rendered)

How it works

It works by having a NodeJS service on the same server that can render the React components. The Python just uses a simple HTTP API to send the context and the file path over to the service, and it responds with the rendered HTML.

Documentation

Installation

npm install react-render-service --save
pip install react-render-client

Optional: Point it to the service in your settings.py

REACT_SERVICE_URL = 'http://localhost:63578/render'

Start the node server which hosts the renderer.

react-service --debug

render_component()

Renders a component to its initial HTML.

Returns a RenderedComponent instance, which can be passed directly into templates to output the component's HTML.

Arguments:

  • path_to_source — a path to a JS or JSX file which exports the component. If the path is relative, django's static file finders will be used to find the file.
  • props optional — a dictonary that will be serialised to JSON and passed to the component during the renderering process.
  • to_static_markup optional — a boolean indicating that React's renderToStaticMarkup method should be used for the rendering. Defaults to False, which causes React's renderToString method to be used.
  • json_encoder optional — a class which is used to encode the JSON which is sent to the renderer. Defaults to django.core.serializers.json.DjangoJSONEncoder.

RenderedComponent

The result of rendering a component to its initial HTML. RenderedComponents can be passed directly into templates where they output the generated HTML.

# Render the component
my_component = render_component(...)

# Print the generated HTML
print(my_component)
<!-- Insert the generated HTML into your template -->
{{ my_component }}

RenderedComponents have a helper method, render_props, which outputs your JSON-serialized props. This allows you to reuse the encoded form of your props on the client-side.

<script>
    var myProps = {{ my_component.render_props }};
</script>

Running the tests

mkvirtualenv react-render
pip install .
cd tests
npm install
npm run react-render &
cd ..
python runtests.py

react-render's People

Contributors

markfinger avatar mic159 avatar defrex avatar matthewryanscott avatar pirate avatar

Watchers

David DeWitt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.