0.3.2 • Published 6 years ago

@cvr/firespace v0.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

Introduction

Firespace is for developers who want to quickly develop applications using Firebase, without all of the overhead. Currently only targeting the firebase real time database. 1kb gzipped!

npm install @cvr/firespace
yarn add @cvr/firespace

Step 1 - Set the firebase config in your root file

import { setConfig } from '@cvr/firespace';

setConfig({
    apiKey: '<APIKEY>',
    databaseURL: '<DATABASEURL>',
});

// you can also extend it
import 'firebase/auth';

const firebase = setConfig({
    apiKey: 'xx',
    databaseURL: 'xx',
    authDomain: 'xx',
});

const auth = firebase.auth();

Step 2 - Use it

import { useSpace } from '@cvr/firespace';

export default function App() {
    const [todos, space] = useSpace('todos');

    return (
        <div>
            <h1>Todo</h1>
            <AddTodo space={space} />
            <Todos todos={todos} space={space} />
        </div>
    );
}

function AddTodo({ space }) {
    const [text, setText] = useState('');
    const handleAddClick = async () => {
        if (text) {
            await space.add({ text, done: false });
            setText('');
        }
    };
    return <input value={text} onChange={e => setText(e.target.value)} placeholder="What to do next" />;
}

function Todos({ todos, space }) {
    const todosList = Object.entries(todos || {});
    return (
        <div>
            {todosList.map(([id, todo]) => (
                <Todo key={id} id={id} todo={todo} space={space} />
            ))}
        </div>
    );
}

function Todo({ todo, id, space }) {
    return (
        <div onClick={() => space.update(id, { done: !todo.done })}>
            <span>{todo.text}</span>
            <button
                onClick={e => {
                    e.stopPropagation();
                    space.delete(id);
                }}
            >
                delete
            </button>
        </div>
    );
}

Simple Api

import { useSpace } from '@cvr/firespace';

function Component() {
    const [todos, space] = useSpace('todos', ref => {
      //optionally, return custom ref
      return ref.orderByValue()
    });

    const id = await space.add({ text: 'Install it', done: false });
    await space.update(id, { done: true });
    await space.delete(id);
    space.loading;
    space.error;
}

Try it

Edit Firespace Example

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.3.0-beta.2

6 years ago

0.3.0-alpha.9

6 years ago

0.3.0-alpha.8

6 years ago

0.3.0-alpha.7

6 years ago

0.3.0-alpha.6

6 years ago

0.3.0-alpha.5

6 years ago

0.3.0-alpha.4

6 years ago

0.3.0-alpha.3

6 years ago

0.3.0-alpha.2

6 years ago

0.3.0-alpha.1

6 years ago

0.3.0-alpha.0

6 years ago

0.3.0-beta.1

6 years ago

0.3.0-beta.0

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago