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

@bloko/react

v0.0.10

Published

<p align="center"> <a href="https://bloko.dev"> <br /> <img src="https://user-images.githubusercontent.com/7120471/80561131-d98be300-89b9-11ea-9956-679a406a387e.png" alt="Logo Bloko" height="60"/> <br /> <sub><strong>Build modular applications

Downloads

46

Readme

Travis build Codecov coverage NPM version

Bloko is currently under heavy development, but can be installed by running:

npm install --save @bloko/react

Quick example

import React, { useState } from 'react';
import Bloko, { useBlokoStore } from '@bloko/react';

const User = Bloko.create({
  name: '',
});

const Store = Bloko.createStore({
  key: 'store',
  state: {
    user: {
      type: User,
      setter: true,
    },
  },
  actions: {},
});

function App() {
  const [state, actions] = useBlokoStore(Store);
  const [name, setName] = useState('');

  function saveName() {
    actions.setUser({ name });
  }

  return (
    <div>
      <input value={name} onChange={e => setName(e.target.value)} />
      <button onClick={saveName}>Save</button>
      <div>User store name: {state.user.name}</div>
    </div>
  );
}

Bloko Provider

To give @bloko/js the necessary context to work properly Bloko.Provider will have to be used on root app React component.

Bloko.Provider is a React Component and needs an array of Bloko Store.

// React entry file
import React from 'react';
import ReactDOM from 'react-dom';
import Bloko from '@bloko/react';
import App from './App';
import Auth from './blokos/Auth';
import Users from './blokos/Users';

const blokos = [Auth, Users];

ReactDOM.render(
  <Bloko.Provider blokos={blokos}>
    <App />
  </Bloko.Provider>,
  document.getElementById('root')
);

API

useBloko(bloko)

A React hook that helps handle Bloko units.

Arguments

Returns a tuple of [state, update]:

  • state - Represents the current state of the Bloko Unit
  • update(value: any) - A function to ease updates on Bloko Unit using partial states, full states or new value instance.

Example

import React from 'react';
import Bloko, { useBloko } from '@bloko/react';

const Child = Bloko.create({
  name: '',
});

const Parent = Bloko.create({
  name: '',
  child: Child,
});

function App() {
  const [parent, setParent] = useBloko(Parent);
  // => null

  // You could be more explicit using initial: false
  const [parent, setParent] = useBloko({ type: Parent, initial: false });
  // => null

  // Use initial: true to start with an object with default values
  const [parent, setParent] = useBloko({ type: Parent, initial: true });
  // => { name: '', child: { name: '' } }

  setParent({ name: 'Parent' });
  // => { name: 'Parent', child: { name: '' } }

  // Partial states are allowed
  setParent({ child: { name: 'Child' } });
  // => { name: 'Parent', child: { name: 'Child' } }

  setParent(null);
  // => null

  // When updates coming through null state
  // Bloko instance will be called again
  // and set result with default values
  setParent({ name: 'Parent' });
  // => { name: 'Parent', child: { name: '' } }
}

useBlokoStore(blokoStore)

A React hook that subscribes to state changes from an existing Bloko Store and also provide its actions to allow user interactions.

Arguments

Returns a tuple of [state, actions]:

  • state - Represents the current state of the Bloko Store. It is a partial representation of global state.
  • actions - A collection of Bloko Store functions to interact with global state.

Example

import React from 'react';
import Bloko, { useBlokoStore } from '@bloko/react';

const User = Bloko.create({
  name: '',
});

const Store = Bloko.createStore({
  key: 'store',
  state: {
    user: {
      type: User,
      setter: true,
    },
  },
  actions: {
    getUser: {
      // Simulate an async request with data.name
      request: () =>
        Promise.resolve({
          data: { name: 'John' },
        }),
      resolved(data) {
        return {
          user: {
            name: data.name,
          },
        };
      },
    },
  },
});

function App() {
  const [state, actions] = useBlokoStore(Store);

  // => state { user: { name: '' }, getUser: { loading: undefined, error: '' } }
  // => actions { setUser(), getUser() }
}