Coder Social home page Coder Social logo

react-slotify's Introduction

React-slotify

Build Status npm bundle size version

MIT License

A tiny way to use Slots concept in modern React apps.

Why react-slotify:

  • ✅ Replaces "renderProps" concept in a semantic way
  • ✅ Written in TypeScript with type-safe API
  • ✅ Lightweight (~500B)
  • ✅ Simple

Installation

Install using your package manager:

npm install --save react-slotify

yarn add react-slotify

Usage

In a component where you want to hold slots in, create and place a slot. You may have as many slots as you want.

import {createSlot} from 'react-slotify';

export const MySlot = createSlot();

export const Component = ({children}) => {
  return (
    <div>
      This component contains slot:
      
      <MySlot.Renderer childs={children}>
        This is default slot content
      </MySlot.Renderer>
      
      <div>It also renders children: {children}</div>
    </div>
  );
};

Import your component and it's slots and use:

import {Component, MySlot} from './component';

const App = () => {
  return (
    <div>
      <Component>
        <MySlot>Slotted content</MySlot>
        Other content
      </Component>
    </div>
  );
};

Passing props back from inside slot

If your slot conent need some params from inside such as disabled state, you are welcome to parametrize it:

export const Slot = createSlot<{myParam: string}>();

export const Component = ({children}) => (
  <MySlot.Renderer childs={children} myParam="123">
    This is default slot content
  </MySlot.Renderer>
);

...

const App = () => {
  return (
    <div>
      <Component>
        <MySlot>
          {params => (<div>Param is {params.myParam}</div>)}
        </MySlot>
      </Component>
    </div>
  );
};

react-slotify's People

Contributors

andrey-skl avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.