React Universal Interface for Component Patterns

0.6.2 · abandoned · verified Sun Apr 19

React Universal Interface (react-universal-interface) is a lightweight utility library designed to simplify the creation of React components that can be consumed via multiple popular patterns, including Function-as-a-Child (FaCC), render props, component props, and Higher-Order Components (HOCs). It offers two primary functions: `render` for processing various child/prop definitions within a component's render method, and `createEnhancer` for generating HOCs. The package's current stable version is 0.6.2, released in May 2020. Given the lack of updates since then, the project appears to be abandoned, with no active release cadence. Its key differentiator was providing a unified API to handle diverse component interaction paradigms, a common challenge in the evolving React ecosystem before Hooks became the dominant approach for logic reuse.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the `MyData` component leveraging `render` to support FaCC, render prop, component prop, and prop injection patterns, alongside `createEnhancer` for HOCs.

import React, { Component } from 'react';
import { render, createEnhancer, UniversalProps } from 'react-universal-interface';

interface MyDataState {
  counter: number;
}

interface MyDataProps extends UniversalProps<MyDataState> {}

class MyData extends Component<MyDataProps, MyDataState> {
  state = { counter: 0 };

  componentDidMount() {
    this.interval = setInterval(() => {
      this.setState(prevState => ({ counter: prevState.counter + 1 }));
    }, 1000);
  }

  componentWillUnmount() {
    clearInterval(this.interval);
  }

  interval: NodeJS.Timeout | undefined;

  render() {
    return render(this.props, this.state);
  }
}

const MyChildComponent: React.FC<MyDataState> = ({ counter }) => (
  <div>Current counter: {counter}</div>
);

// Usage with Function-as-a-Child (FaCC)
function App() {
  return (
    <div>
      <h2>FaCC Example:</h2>
      <MyData>{(state) => <MyChildComponent {...state} />}</MyData>

      <h2>Render Prop Example:</h2>
      <MyData render={(state) => <MyChildComponent {...state} />} />

      <h2>Component Prop Example:</h2>
      <MyData comp={MyChildComponent} />
      <MyData component={MyChildComponent} />

      <h2>Prop Injection Example:</h2>
      <MyData>
        <MyChildComponent />
      </MyData>
    </div>
  );
}

// Example of Higher Order Component usage
const withCounter = createEnhancer(MyData, 'data');
const EnhancedChild = withCounter(MyChildComponent);

function HOCApp() {
    return (
        <div>
            <h2>HOC Example:</h2>
            <EnhancedChild /> {/* MyChildComponent will receive 'data' prop from MyData */}
        </div>
    );
}

export default App;

view raw JSON →