# react-rebase

> react-rebase React component

Latest version **1.0.5** (published 2017-11-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-rebase
pnpm add react-rebase
yarn add react-rebase
bun add react-rebase
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2017-11-03 |
| First published | 2017-06-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | gott |
| Keywords | react-component |

## Links

- npm: https://www.npmjs.com/package/react-rebase
- Repository: https://github.com/go1t/react-rebase
- Homepage: https://github.com/go1t/react-rebase#readme
- Issues: https://github.com/go1t/react-rebase/issues
- npm.io page: https://npm.io/package/react-rebase

## Dependencies (3)

- [sinon](https://npm.io/package/sinon.md) ^2.3.4
- [enzyme](https://npm.io/package/enzyme.md) ^2.8.2
- [react-addons-test-utils](https://npm.io/package/react-addons-test-utils.md) ^15.6.0

## Recent versions

- 1.0.5 (latest) — 2017-11-03
- 1.0.4 — 2017-11-03
- 1.0.3 — 2017-11-03
- 1.0.2 — 2017-09-01
- 1.0.1 — 2017-06-22
- 1.0.0 — 2017-06-18

## README

# react-rebase

[![Travis][build-badge]][build]
[![npm package][npm-badge]][npm]
[![Coveralls][coveralls-badge]][coveralls]

## What is this?
[Re-base](https://github.com/tylermcginnis/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](https://github.com/tylermcginnis/re-base#postendpoint-options) and [bindToState](https://github.com/tylermcginnis/re-base#bindtostateendpoint-options) 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](https://hackernoon.com/react-stateless-functional-components-nine-wins-you-might-have-overlooked-997b0d933dbc). 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
```javascript
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:
```javascript
// 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:

```javascript
// 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

```javascript
// 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](https://github.com/tylermcginnis/re-base#bindtostateendpoint-options) specified there.

Example: 
```javascript
const mapRefDataToProps = (props) => ({ 
  items: { 
    path: 'path/to/items', 
    options: { asArray: true },  // optional
  }
});
```

2. `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: 
```javascript
const mapBaseActionsToProps = (base, props) => ({ 
  addItems: (id, newItem) => base.post(`path/to/items/${id}`, newItem)
});
```

[build-badge]: https://img.shields.io/travis/user/repo/master.png?style=flat-square
[build]: https://travis-ci.org/user/repo

[npm-badge]: https://img.shields.io/npm/v/npm-package.png?style=flat-square
[npm]: https://www.npmjs.org/package/npm-package

[coveralls-badge]: https://img.shields.io/coveralls/user/repo/master.png?style=flat-square
[coveralls]: https://coveralls.io/github/user/repo

---
_Source: https://npm.io/package/react-rebase · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
