Skip to content

Commit 9515097

Browse files
Render a shell Element component on the server (#21)
1 parent 7f0ccf2 commit 9515097

File tree

3 files changed

+342
-269
lines changed

3 files changed

+342
-269
lines changed

src/components/createElementComponent.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @flow
2-
/* eslint-disable react/forbid-prop-types */
2+
/* eslint-disable react/forbid-prop-types, react/no-unused-prop-types */
33
import React, {useRef, useEffect, useLayoutEffect} from 'react';
44
import PropTypes from 'prop-types';
55
import {useElementsContextWithUseCase} from './Elements';
@@ -42,10 +42,10 @@ const useCallbackReference = (cb: MixedFunction) => {
4242
};
4343
};
4444

45-
const createElementComponent = (type: string) => {
45+
const createElementComponent = (type: string, isServer: boolean) => {
4646
const displayName = `${capitalized(type)}Element`;
4747

48-
const Element = (props: Props) => {
48+
const ClientElement = (props: Props) => {
4949
const {
5050
id,
5151
className,
@@ -122,6 +122,16 @@ const createElementComponent = (type: string) => {
122122
return <div id={id} className={className} ref={domNode} />;
123123
};
124124

125+
// Only render the Element wrapper in a server environment.
126+
const ServerElement = (props: Props) => {
127+
// Validate that we are in the right context by calling useElementsContextWithUseCase.
128+
useElementsContextWithUseCase(`mounts <${displayName}>`);
129+
const {id, className} = props;
130+
return <div id={id} className={className} />;
131+
};
132+
133+
const Element = isServer ? ServerElement : ClientElement;
134+
125135
Element.propTypes = {
126136
id: PropTypes.string,
127137
className: PropTypes.string,

0 commit comments

Comments
 (0)