GithubHelp home page GithubHelp logo

curlywurlycraig / react-naughty Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 303 KB

A set of combinator utility components to do naughty things with hooks, and enable a compositional approach to component logic.

HTML 14.43% CSS 7.80% JavaScript 77.78%

react-naughty's Introduction

Small higher-order component utilities for bending the React hook rules.

Why?

People on the internet keep telling you not to call hooks in loops or conditionally. They are right, but you feel like you should be able to somehow that isn't buggy.

Perhaps you are using a hook you cannot modify and you need to call it an unknown times with different arguments.

Or another hook you cannot modify performs an expensive operation that you only want to take place when a button is clicked.

This project enables such patterns by thinking differently about what components actually are.

Usage

Calling a hook conditionally with the <Use /> component

You cannot do the following:

let hookResult = null;
if (something) {
    hookResult = useMyHook();
}

return (
  <p>{ hookResult }</p>
);

But with <Use />:

return (
    { something ? (
    <Use hook={useMyHook}>
        {(hookResult) => <p>{ hookResult }</p>}
    </Use>
    ) : null }
);

Or by passing a when prop, simplifying the conditional logic:

return (
    <Use
      when={something}
      hook={useMyHook}>
        {(hookResult) => <p>{ hookResult }</p>}
    </Use>
)

Calling a hook many times with <UseTimes />

You cannot do the following:

const hookResults = new Array(10).fill(null).map(() => useMyHook());

return <p>{ hookResults.length }</p>

But with <UseTimes />:

return <UseTimes n={10} hook={useMyHook}>
    {(results) => <p>{ results.length }</p>}
</UseTimes>

Calling the same hook many times with different arguments, using <UseAll />

TODO

Calling multiple hooks with different arguments, binding the results in an object, using <UseLet />

TODO

How?

TODO: Write about how hook rules can be bent by having their wrapper component be conditionally rendered. The hook is always coupled with the component.

react-naughty's People

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.