Skip to content

Instantly share code, notes, and snippets.

@hnq90
Created March 28, 2019 02:01
Show Gist options
  • Select an option

  • Save hnq90/074053fde47fec774bb4aea931aff6b1 to your computer and use it in GitHub Desktop.

Select an option

Save hnq90/074053fde47fec774bb4aea931aff6b1 to your computer and use it in GitHub Desktop.
renderNode.js
import React from 'react';
const renderNode = (Component, content, defaultProps) => {
if (content == null || content === false) {
return null;
}
if (React.isValidElement(content)) {
return content;
}
if (typeof content === 'function') {
return content();
}
// Just in case
if (content === true) {
return <Component {...defaultProps} />;
}
if (typeof content === 'string' || typeof content === 'number') {
return <Component {...defaultProps}>{content}</Component>;
}
return <Component {...defaultProps} {...content} />;
};
export default renderNode;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment