Advertisement
Guest User

React Inject Props

a guest
Aug 2nd, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from "react";
  2.  
  3. const propsAvailable = {
  4.     http: 'http',
  5.     config: 'config',
  6. };
  7.  
  8. /**
  9.  * Injects the requested props from available props
  10.  *
  11.  * @param props
  12.  * @returns {function(*=): {new(): Proxy, prototype: Proxy}}
  13.  */
  14. export function injectProps(props = []) {
  15.     // get the props requested
  16.     const injects = {};
  17.     for (const prop in props) {
  18.         if (propsAvailable[props[prop]] !== undefined) {
  19.             injects[props[prop]] = propsAvailable[props[prop]];
  20.         }
  21.     }
  22.  
  23.     // return the component through a function wrapper
  24.     return function (Component) {
  25.         return class Proxy extends React.Component {
  26.             render() {
  27.                 return React.createElement(Component, {
  28.                     ...injects,
  29.                     ...this.props,
  30.                 });
  31.             }
  32.         };
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement