New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

elefant-state

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

elefant-state

Elefant is a simple global state management for React projects

  • 1.1.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Elefant

Simple Global State Management

Stability, Loyalty, Intelligence, and Reliability

Elefant is a simple, quick, and easy global state management for React projects. In this README you will see a walkthrough and summary of how Elefant works with your project.

Three Simple Steps:

  1. Create reducer function and initial state.
  2. Wrap app in a provider component passing above two items.
  3. Use the context/global state in any component with Reacts useContext()

Install

npm i elefant-state

This project exports two items:

ElefantContext

The ElefantContext is the context object that is passed into Reacts useContext() on each component.

ElefantProvider

The <ElefantProvider /> makes the state available to any nested components. Since any React component in a React app can use context, most applications will use an <ElefantProvider /> at the top level, with the entire app inside of it. This component accespts two props.

PropTypeDescription
reducerFunctionA function that specifies how the application's state changes in response to actions.
initialStateObjectOptional object to set as the initial state when the gloabl state is created.

Example

Setup:

src/State.js

export const initialState = { count: 0 }

export const reducer = (state, action) => {
  switch (action.type) {
    case 'increase':
      return {
        ...state,
        count: state.count + 1,
      }
    case 'decrease':
      return {
        ...state,
        count: state.count - 1,
      }
    case 'reset':
      return {
        ...state,
        count: initialState.count
      }
    default:
      return state
  }
}

src/index.js

import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import { initialState, reducer } from './State'
import { ElefantProvider } from 'elefant'

ReactDOM.render(
  <ElefantProvider reducer={reducer} initialState={initialState}>
    <App />
  </ElefantProvider>,
  document.getElementById('root'),
)

src/App.js

const App () => {
  return (
    <div>
      <Counter />
    </div>
  )
}

export default App
Use:

src/Counter.js

import React, { useContext } from 'react'
import { ElefantContext } from 'elefant'

export const Counter () => {
  const [state, dispatch] = useContext(ElefantContext)

  const handleDecrease = () => dispatch({ type: 'decrease' })
  const handleIncrease = () => dispatch({ type: 'increase' })
  const handleReset = () => dispatch({ type: 'reset' })

  return (
    <div>
      <p>{state.count}</p>
      <button onClick={handleDecrease}>Decrease</button>
      <button onClick={handleReset}>Reset</button>
      <button onClick={handleIncrease}>Increase</button>
    </div>
  )
}

Keywords

FAQs

Package last updated on 01 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc