npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-rebase

v1.0.5

Published

react-rebase React component

Downloads

5

Readme

react-rebase

Travis npm package Coveralls

What is this?

Re-base is a great Firebase library that makes it easy to use Firebase with react components by binding the Firebase data with the component's state. react-rebase is a wrapper of re-base that provides an ability to bind the data to props instead!

Note that this library totally depends on Re-base library to do everything! Some level of knowledge of re-base API is needed, at least checkout post and bindToState function.

Also, it's incredibly IMMATURE and somewhat a work in progress that I barely touch anymore. Use it at your own risk!

Why react-rebase?

Since Re-base binds Firebase data to state, we are basically forced to have a stateful component connect to it. However, this is an era of stateless functional component. To have an otherwise-stateless component become stateful just to connect with Firebase could leave some bad taste in the mouths of stateless functional component lovers like me. Obviously one could separate the firebase-connected component to a container component solely for providing Firebase data and callback functions to update state (which interns update those data). However, this approach doesn't scale very well since there will be a need to pass those data and callback functions around a lot. It's a similar problem you will likely face if you try to use redux without react-redux library.

All things considered, since there is no such convenient library that would simply bind Firebase reference to props, I created react-rebase to do just that. react-rebase will abstract the state binding to a higher order component and provide the data to props directly.

Installation

To install, simply do npm install --save react-rebase or yarn add react-rebase

Usage

rebaseConnect(mapRefDataToProps, mapBaseActionsToProps)(Component);

where mapRefDataToProps is a function that takes in props and returns a props object made from the ref data that is bound to the HoC state, and mapBaseActionsToProps is a function that takes in Re-base object and props then returns a props object made from mapBaseActionsToProps. The details on how these parameters look like are below.

Note: The API is heavily inspired by react-redux.

Example

Let's say we are working on a counter component, probably something like this:

// Counter.jsx
const Counter = ({ count, updateCount }) => (
  <div>
    Current value: {count}
    <button onClick={() => updateCount(count+1)}>Add</button>
  </div>  
);

Now we want to connect it to Firebase for real-time capabilities. First we will need to instantiate the Re-base and pass it to the top level RebaseProvider. Like this:

// App.jsx
import Rebase from "re-base";
import { RebaseProvider } from "react-rebase";
import Counter from './Counter.jsx'

const base = Rebase.createClass({
  ...
});

const App = () => (
  <RebaseProvider instance={base}>
    <Counter /> 
  </RebaseProvider>
)

Now that we provided RebaseProvider with the re-base instance, let's make some change to our Counter.jsx so it can connect to Firebase. For this example we will assume that the data lives at '/count' endpoint

// Counter.jsx
import { rebaseConnect } from 'react-rebase';

const Counter = ({ count, updateCount }) => (
  <div>
    Current value: {count}
    <button onClick={() => updateCount(count+1)}>Add</button>
  </div>  
);

const COUNT_PATH = '/count';

export default rebaseConnect(
  (props) => ({
    count: { path: COUNT_PATH }, // this becomes a `count` prop
  }),
  (base) => ({
    updateCount: (newValue) => base.post(COUNT_PATH, newValue), // this becomes an `updateCount` prop
  })
)(Counter);

And that's it! Now our counter will be connected to Firebase.

Documentation (WIP)

rebaseConnect(mapRefDataToProps, mapBaseActionsToProps)

Connect the component with the props constructed from Firebase data and re-base actions. The user needs to provide two functions:

  1. mapRefDataToProps(props) This function should return an object where the key is the prop name and the value is an object containing Firebase path and options specified there.

Example:

const mapRefDataToProps = (props) => ({ 
  items: { 
    path: 'path/to/items', 
    options: { asArray: true },  // optional
  }
});
  1. mapBaseActionsToProps(base, [props]) This function should return an object where the key is the prop name and the value is a function that potentially mutates the data on firebase.

Example:

const mapBaseActionsToProps = (base, props) => ({ 
  addItems: (id, newItem) => base.post(`path/to/items/${id}`, newItem)
});