зеркало из https://github.com/github/docs.git
5bd96598df | ||
---|---|---|
.. | ||
README.md |
README.md
React (Experimental)
The files in this directory are React components. We can use these components within Markdown files by adding interactive: true
frontmatter to a Markdown file. Then, using the following syntax to embed the component:
<!--react-->
<ReactComponentHere some='prop' other={`prop`} />
<!--end-react-->
Theoretically React can be embedded anywhere on the site with a little bit of creativity but that is yet to be tested.
Defining new components
Start by adding frontmatter interactive:true
to any Markdown file that you want to support React. This prevents React render time from slowing down any other page and keeping the impact on performance very low.
- Create the component in the
./react
directory - Register the component for webpack in
./javascripts/index.js
so the component works with client side rendering - Register the component in
./lib/react/engine.js
so the component works with server side rendering - If the component needs to be evaluated client side, see this example for how to ensure the client side component rendered by the server gets Hydrated.
A Complete Example
The following example demonstrates the addition of an interactive CodeEditor on one of our pages:
- Defining the CodeEditor React Component
- Configuring the CodeEditor for client-side rendering
- Configuring the CodeEditor for server-side rendering
- CodeEditor added to a markdown file
Gotchas
- When requiring React components from npm you will often need to explicitly call
require('component').default
to make sure you get the component - Some code examples you get from external React packages won't work out of the box, you often need to dig into the module and determine whether it's exporting to default or not which will in many cases cause you to need to
require('package').default
import
doesn't always work. Userequire
for a more consistent experience with React in this codebase.- If components require you to load CSS, you need to load that CSS in the
javascripts/index.js
file as shown here