2.0.11 • Published 1 year ago

storken v2.0.11

Weekly downloads
17
License
MIT
Repository
github
Last release
1 year ago

Storken

Storken is a hook-based fully extendable and minimal state manager for React.

Only supported for React v16.8+

Installation

# for yarn
yarn add storken

# for npm
npm install storken

Getting Started

First, create a store:

import { create as createStore } from 'storken'

const [useStorken, getStorken, setStorken, GlobalStorken] = createStore()

creatStore will give an array of contains these in order of:

  • A hook function for access the state inside of a React component
  • A function for access the state from everywhere.
  • A function for set the state from everywhere.
  • A class for access the whole Store from everywhere.

Using the hook

Using of Storken's hook is designed like using React's built-in useState as simply.

Storken's hook requires only 1 parameter which gets name of the state for know the options if exist in createStore arguments.

In fact, if you want to access the state globally from everywhere, almost you don't need to learn anything new in this respect.

Except one thing, you should define initial value of the state in createStore if you don't want to default value is undefined.

import { create as createStore } from 'storken'


const [useStorken] = createStore({
  initialValues: {
    counter: 0
  }
})


const Counter = () => {
  const [count, setCount] = useStorken('counter')

    return (
      <>
        <div id="count">{count}</div>
        <button onClick={() => setCount(count + 1)}>Increase value</button>
        <button onClick={() => setCount(count - 1)}>Decrease value</button>
      </>
    )
}

If you want to set value of the state to initial value, just get a reset function after the set function.

const Counter = () => {
  const [count, setCount, resetCount] = useStorken('counter')

    return (
      <>
        <div id="count">{count}</div>
        <button onClick={() => resetCount()}>Reset counter</button>

        <button onClick={() => setCount(count + 1)}>Increase value</button>
        <button onClick={() => setCount(count - 1)}>Decrease value</button>
      </>
    )
}

Ok, let's see what it can do really?!

Storken offers 3 main feature.

  1. Getters
  2. Setters
  3. Plugins

1- Getters

  • Attach a getter function to the state. The getter function must be in getters section of createStore function arguments with the name of the state. Referred as weather.

  • Storken provides a loading status while executing the getter. Referred as loading.

  • Storken provides a function which can re-call the getter function. Referred as update.
import { create as createStore } from 'storken'


const [useStorken] = createStore({
  getters: {
    // Assume the service provides a json like this: 
    // { place: "San Francisco, NC", "temperature": 10, "measurement": "celsius" }
    weather: () => fetch('http://myweatherservice.com/').then(res => res.json())
  }
})

const WeatherPage = () => {
  const [weather,,,loading, update] = useStorken('weather')

  return (
    <>
      {loading 
        ? <div id="loading">Please wait...</div> 
        : (
          <>
            <div id="place">{weather.place}</div>
            <div id="temperature">{weather.temperature} ({weather.measurement === 'celsius' ? '°C' : '°F'})</div>
            <button id="update" onClick={() => update()}>🔄 Refresh</button>
          </>
        )
      }
    </>
  )
  }

Quick Tip: There are different usage types of the hook. You can take look from here

Advanced usage

First parameter of the getter function is Storken object belong to the state.
The rest of the parameters can be getter specific optional parameters.

import { useState } from 'react'
import { create as createStore } from 'storken'


const [useStorken] = createStore({
  getters: {
    // Assume the service provides a json like this: 
    // { place: "San Francisco, NC", "temperature": 10, "measurement": "celsius" }
    weather: (stork, place) => {
      return fetch('http://myweatherservice.com/' + place).then(res => res.json())
    }
})

const WeatherPage = () => {
  const [place, setPlace] = useState('San Francisco, NC')
  const [weather,,,loading, update] = useStorken('weather', place)

  return (
    <>
      {loading 
        ? <div id="loading">Please wait...</div> 
        : (
          <>
            <input type="text" value={place} onChange={({ target: { value } }) => setPlace(value)} />
            <div id="temperature">{weather.temperature} ({weather.measurement === 'celsius' ? '°C' : '°F'})</div>
            <button id="update" onClick={() => update()}>🔄 Refresh</button>
          </>
        )
      }
    </>
  )
  }

Storken object will be introduced additionally. For now, don't get this confused.

The place parameter could be just a string or anything. But we did it separated state. In this way runs getter function every changes of place value and changes weather data instantly.

2- Setters

Attaching a setter to a state, triggers the setter function every time setted the state.

import { create as createStore } from 'storken'


const [useStorken] = createStore({
  setters: {
    language: (stork) => {
      const { value } = stork
      fetch('http://myapi.com/user/language/' + value)
    }
})

const LanguageComponent = () => {
  const [language, setLanguage] = useStorken('language')

  return (
    <>
      <div id="chosenLanguage">{language}</div>
      <button onClick={() => setLanguage('en')}>ENGLISH</button>
      <button onClick={() => setLanguage('tr')}>TURKISH</button>
    </>
  )
}

3- Plugins

Plugins are the best way to extend functions of this minimal library.

Official Plugins

When you want to use a plugin just define it into the plugins section of createStore arguments, as follows:

import { create as createStore } from 'storken'
import StorkenActions from 'storken-actions'

const [useStorken] = createStore({
  plugins: {
    actions: StorkenActions
  }
})

The actions keyword may be change by arbitrary, it just defines usage name of the plugin.

If need to specify a configuration for a plugin then do like this

import { create as createStore } from 'storken'
import StorkenActions from 'storken-actions'

const [useStorken] = createStore({
  plugins: {
    actions: [StorkenActions, { ...actionsConfig }]
  }
})

You can find more use cases on their pages of plugins.

Some Tricks

Every state that created by Storken is a Storken object 🙃 For this reason createStorken gets a storkenOptions object that contains storken specific configurations by name. So you can configure every state in.

Change default loading value

The loading value is false while initiating the state, if the getter exists then it turns to true while executing the getter function, after it ends the loading value becomes false again.

But in some cases, it must be true as default while initiating whence we don't want to show empty things to user.

Simply specify what will be the loading state.

import { create as createStore } from 'storken'

const [useStorken] = createStore({
  storkenOptions: {
    weather: {
      loading: true
    }
  }
})

Using the hook as simpler

Storken provides more features than built-in useState, therefore hook's return length gets longer according to useState.

  1. Access the return value with keys as follows
const AnyComponent = () => {
  const {
    value,
    loading,
    update
  } = useStorken('weather')
}
const AnyComponent = () => {
  const {
    value,
    loading,
    plugins: {
      actions
    }
  } = useStorken('weather')

  const onClick = () => actions.changeTemperature('fahrenheit')

  return (
    <>
      <button onClick={onClick}>Fahrenheit</button>
    </>
  )
}
  1. Reproduce your own hook like this
import { create as createStorken } from 'storken'
import StorkenActions from 'storken-actions'

const [useStorken] = createStorken({
  plugins: {
    actions: StorkenActions
  }
})

const useActions = (name, ...args) => useStorken(name, ...args).plugins.actions

License

Distributed under the MIT License.

Contribution

You can contribute by fork this repository and make pull request.

2.1.2-beta.0

1 year ago

2.1.1-beta.9

1 year ago

2.1.1-beta.1

1 year ago

2.1.1-beta.2

1 year ago

2.1.1-beta.3

1 year ago

2.1.1-beta.4

1 year ago

2.1.1-beta.5

1 year ago

2.1.1-beta.6

1 year ago

2.1.1-beta.7

1 year ago

2.1.1-beta.8

1 year ago

2.1.1-beta.0

2 years ago

2.1.0-beta.18

2 years ago

2.1.0-beta.17

2 years ago

2.1.0-beta.16

2 years ago

2.1.0-beta.15

2 years ago

2.1.0-beta.14

2 years ago

2.1.0-beta.13

2 years ago

2.1.0-beta.12

2 years ago

2.1.0-beta.11

2 years ago

2.1.0-beta.10

2 years ago

2.1.0-beta.19

2 years ago

2.1.0-beta.9

2 years ago

2.1.0-beta.1

2 years ago

2.1.0-beta.0

2 years ago

2.1.0-beta.3

2 years ago

2.1.0-beta.2

2 years ago

2.1.0-beta.5

2 years ago

2.1.0-beta.4

2 years ago

2.1.0-beta.7

2 years ago

2.1.0-beta.6

2 years ago

2.1.0-beta.8

2 years ago

2.1.0-ts.8

2 years ago

2.1.0

2 years ago

2.0.11

2 years ago

2.0.10

2 years ago

2.0.9

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.8

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.2.0

4 years ago

1.1.32

4 years ago

1.1.31

4 years ago

1.1.29

4 years ago

1.1.28

4 years ago

1.1.30

4 years ago

1.1.27

4 years ago

1.1.26

4 years ago

1.1.25

4 years ago

1.1.24

4 years ago

1.1.23

4 years ago

1.1.22

4 years ago

1.1.19

4 years ago

1.1.21

4 years ago

1.1.20

4 years ago

1.1.18

4 years ago

1.1.17

4 years ago

1.1.16

4 years ago

1.1.15

4 years ago

1.1.14

4 years ago

1.1.13

4 years ago

1.1.12

4 years ago

1.1.11

4 years ago

1.1.10

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.54

4 years ago

1.0.53

4 years ago

1.0.52

4 years ago

1.0.51

4 years ago

1.0.49

4 years ago

1.0.50

4 years ago

1.0.48

4 years ago

1.0.47

4 years ago

1.0.46

4 years ago

1.0.45

4 years ago

1.0.44

4 years ago

1.0.43

4 years ago

1.0.42

4 years ago

1.0.41

4 years ago

1.0.40

4 years ago

1.0.39

4 years ago

1.0.38

4 years ago

1.0.37

4 years ago

1.0.36

4 years ago

1.0.35

4 years ago

1.0.34

4 years ago

1.0.33

4 years ago

1.0.32

4 years ago

1.0.31

4 years ago

1.0.29

4 years ago

1.0.28

4 years ago

1.0.30

4 years ago

1.0.27

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.22

4 years ago

1.0.23

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.9

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago