Giter Site home page Giter Site logo

phl3bas / react-flow-control Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 2.0 313 KB

React component library for declarative JSX control flow

Home Page: https://www.reactflowcontrol.com

License: MIT License

JavaScript 26.27% EJS 10.10% TypeScript 63.63%
react flow-control component-library components hacktoberfest

react-flow-control's Introduction

React-Flow-Control

React component library for declarative JSX control flow.

react-flow-control aims to replace patterns such as

    data && <Component />

or

    isBoolean ? <ComponentOne /> : <ComponentTwo />

which when nested and the control flow logic gets more complex can become a nightmare to reason with when reading especially with larger codeblocks within the ternary returns. react-flow-control instead places this logic within renderless JSX elements whose sole perpose is to render its children based on various conditions.

the most basis is the Show component.

// This

function myComponent(){
    const {data, loading, error} = useSomeHook()

    return (
       {
           !loading ? 
                !error ?
                    (<ThingIActuallyWantToRender /> ):
                    (<ErrorComponent />)
                : ( <LoadingSpinner />)
       }
    )
}


// becomes



function myComponent(){
    const {data, loading, error} = useSomeHook()

    return (
        <Show when={!loading} fallback={()=> <LoadingSpinner/>}>
            <Show when={!error} fallback={()=><ErrorComponent/>}>
                <ThingIActuallyWantToRender />
            </Show>
        </Show>
    )
}

react-flow-control also provides utilities for loops, and switches.

install

    npm install react-flow-control
    yarn add react-flow-control

Contributing

If theres a control flow you think could be useful in this pattern please make and issue or pr. All contributions are welcome, in the form of bug fixes, docs, tests aswell as features. Below are some features of this repo for those running this package locally.

react-flow-control's People

Contributors

seppwc avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

react-flow-control's Issues

For - keyedBy prop

The current implementation for For's auto-key-ing is naive and frankly stupid as it brings in possible issues with duplicate keys.

a possible solution is to default keying the items in the array by they index, and also including a keyedBy prop that will be a string, referencing a property name in the item which to use as a key

example api

function App(){

  const arr = [
    {
      _id: 'id-string-01',
      name: 'Bob'
    },
    {
      _id: 'id-string-02',
      name: 'Frank'
    },   
    {
      _id: 'id-string-03',
      name: 'James'
    }

  ]

  return (
    <For each={arr} keyedBy={'_id'} >
      {(value)=>(<div>{value.name}</div>)}
    </For>
  )
}

no keyedBy Prop should default to keying the item by its index, not optimal for all cases, but is want is often used.

For - loop over object entires

A common pattern is to loop over object keys or entries, we could probably do this with an of prop that takes an Record<string, T> and loops of Object.keys and/or and entries prop that loops of Object.entries

Improve docs

The docs in the readme could be improved with details about switch and for will also need to create a single page template for a doc site though this will be very small, readme will be enough for now

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.