UNPKG

react-on-rails

Version:

react-on-rails JavaScript for react_on_rails Ruby gem

62 lines (61 loc) 2.85 kB
"use strict"; /* eslint-disable react/prop-types */ Object.defineProperty(exports, "__esModule", { value: true }); exports.default = createReactOutput; const React = require("react"); const isServerRenderResult_1 = require("./isServerRenderResult"); /** * Logic to either call the renderFunction or call React.createElement to get the * React.Component * @param options * @param options.componentObj * @param options.props * @param options.domNodeId * @param options.trace * @param options.location * @returns {ReactElement} */ function createReactOutput({ componentObj, props, railsContext, domNodeId, trace, shouldHydrate, }) { const { name, component, renderFunction } = componentObj; if (trace) { if (railsContext && railsContext.serverSide) { console.log(`RENDERED ${name} to dom node with id: ${domNodeId}`); } else if (shouldHydrate) { console.log(`HYDRATED ${name} in dom node with id: ${domNodeId} using props, railsContext:`, props, railsContext); } else { console.log(`RENDERED ${name} to dom node with id: ${domNodeId} with props, railsContext:`, props, railsContext); } } if (renderFunction) { // Let's invoke the function to get the result if (trace) { console.log(`${name} is a renderFunction`); } const renderFunctionResult = component(props, railsContext); if ((0, isServerRenderResult_1.isServerRenderHash)(renderFunctionResult)) { // We just return at this point, because calling function knows how to handle this case and // we can't call React.createElement with this type of Object. return renderFunctionResult; } if ((0, isServerRenderResult_1.isPromise)(renderFunctionResult)) { // We just return at this point, because calling function knows how to handle this case and // we can't call React.createElement with this type of Object. return renderFunctionResult; } if (React.isValidElement(renderFunctionResult)) { // If already a ReactElement, then just return it. console.error(`Warning: ReactOnRails: Your registered render-function (ReactOnRails.register) for ${name} incorrectly returned a React Element (JSX). Instead, return a React Function Component by wrapping your JSX in a function. ReactOnRails v13 will throw error on this, as React Hooks do not work if you return JSX. Update by wrapping the result JSX of ${name} in a fat arrow function.`); return renderFunctionResult; } // If a component, then wrap in an element const reactComponent = renderFunctionResult; return React.createElement(reactComponent, props); } // else return React.createElement(component, props); }