0.0.8 • Published 7 years ago

react-floodgate v0.0.8

Weekly downloads
8
License
MIT
Repository
github
Last release
7 years ago

The motivation

I have worked on a few client sites and side projects where serialized data is to be displayed concatenated to a given length, with the ability to load more entries after a respective user interaction.

This can easily result in a complicated mixture of Array.splice-ing, potential data mutation, and overly complicated component methods.

Surely there can be a more elegant solution?

This solution

Enter react-floodgate; like its namesake, this component allows for the precise and safe control of resources. Using an ES2015 generator function as the control mechanism and the function-as-child pattern for flexible and developer-controlled rendering, one can load serialized data into react-floodgate, render their desired components, and safely and programmatically iterate through the data as needed.

The inspiration

This project was inspired by Kent Dodd's Downshift, this talk by Ryan Florence, and this blog post by Max Stoiber.

This README file modeled after the Downshift README.

Installation

You can install the package via npm or yarn:

$ yarn add react-floodgate

or

$ npm i --save react-floodgate

Usage

const BasicExample = props => (
  <Floodgate items={["hello","world","!"]} initial={3} increment={1}>
    {
      ({ items }) => (
        <ul>
          {items.map(text => <li key={text}>{text}</li>)}
        </ul>
      )
    }
  </Floodgate>
);

/* Renders:

• hello
• world
• !

*/

const LoadMoreExample = props => (
  <Floodgate items={['foo','bar','baz','buzz','daz','doz']} initial={3} increment={1}>
    {
      ({ items, loadNext, loadComplete }) => (
        <React.Fragment>
          <ul>
            {items.map(text => <li key={text}>{text}</li>)}
          </ul>
          {!loadMore ? <button onClick={loadNext}>Load More</button> : <p>All items loaded!</p>}
        </React.Fragment>
      )
    }
  </Floodgate>
)

/* Renders:

• foo
• bar
• baz

[ Load More ]

*load more button click*

• foo
• bar
• baz
• buzz

[ Load More ]

*load more button click*

• foo
• bar
• baz
• buzz
• daz

[ Load More ]

*load more button click*

• foo
• bar
• baz
• buzz
• daz
• doz

All items loaded!

*/

API

Floodgate props

nametypedefaultdescription
itemsArray\nullThe array of items to be processed by Floodgate
initialInteger5How many items are initially available in the render function
incrementInteger5How many items are added when calling loadNext

render function

Note: the render function uses a single object argument to expose the following values/functions. Use the ES2015 destructuring syntax to get the most of this pattern. (see the Examples section on how to do this)

nametypedefaultdescription
itemsArray\nullState: the subset of items determined by the intitial and increment props
loadCompleteBooleanfalseState: describes if all items have been processed by the Floodgate instance
loadAllFunctionn/aAction: loads all items
loadNextFunctionn/aAction: loads the next set of items
resetFunctionn/aAction: resets the state of the Floodgate instance to the initial state

Examples

Contributors

LICENSE

MIT

1.0.0

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.7

7 years ago

0.5.6

7 years ago

0.5.5

7 years ago

0.5.4

7 years ago

0.5.3

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.3.0

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0-beta

7 years ago

0.0.8

7 years ago

0.0.7-0

7 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago

0.0.0

8 years ago