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

lesley

v0.0.6

Published

Dead simple state manager for React

Downloads

3

Readme

Lesley @BETA

npm License Build Status Coverage Status types: typescript/flow code style: prettier PRs Welcome

Dead simple state manager for React | github.com/Eazymov/Lesley

Installation

Direct <script /> include:

Include Lesley after React.

<script src="path/to/React"></script>
<script src="https://cdn.jsdelivr.net/npm/lesley@latest"></script>

or via unpkg

<script src="path/to/React"></script>
<script src="https://unpkg.com/lesley@latest"></script>

NPM

npm install lesley --save

Yarn

yarn add lesley

Why

The purpose of this library is to make global state management in React as simple as possible

Examples

Basic example

Creating the Store

// store/index.js
import Lesley from 'lesley'

const initialState = {
  // ...data you need
  currentUser: {
    name: 'Jon Doe',
    age: 20,
  },
}

const store = new Lesley.Store(initialState)
const connect = Lesley.createConnector(store)

export { connect }

Connecting component to the store

// components/MyComponent
import React from 'react'

import { connect } from '../../store'

class MyComponent extends React.Component {
  handleInput = event => {
    const { value } = event.target

    this.props.changeUsername(value)
  }

  render() {
    const { username } = this.props

    return <input value={username} onInput={this.handleInput} />
  }
}

const mapState = state => ({
  username: state.currentUser.name,
})

const mapActions = state => ({
  changeUsername(value) {
    state.currentUser.name = value
  },
})
/**
 * You can omit any of arguments or just pass undefined
 * e.g. connect(mapState) or connect(undefined, mapActions)
 */
export default connect(mapState, mapActions)(MyComponent)

Edit this example on codesandbox.io

Sharing actions

Creating actions

// store/actions

const increase = state => {
  state.count += 1;
};

const decrease = state => {
  state.count -= 1;
};

export { increase, decrease };

Connect actions to the component

// components/MyComponent
import React from 'react'
import { bindActions } from 'lesley'

import { connect } from '../../store'
import { increase, decrease } from '../../store/actions'

const MyComponent = ({ count, increase, decrease }) => (
  <div>
    <h3>Count: { count }</h3>
    <button onClick={increase}>Increase</button>
    <button onClick={decrease}>Decrease</button>
  </div>
)

const mapState = state => ({
  count: state.count,
})

const mapActions = state => bindActions({
  increase,
  decrease,
}, state)

export default connect(mapState, mapActions)(MyComponent)

Edit this example on codesandbox.io

Sharing actions via HOCs

// HOC/withUserActions
import api from '../api'
import { connect } from '../store'
/**
 * e.g. the state is: { user: null }
 */
const withUserActions = connect(
  undefined, // omit the first `mapState` argument
  state => ({
    setUser(userProfile) {
      state.user = userProfile
    },
    logout() {
      api.logout()

      state.user = null
    },
  }),
)

export default withUserActions

// components/MyComponent
import React from 'react'

import api from '../../api'
import withUserActions from '../../HOC/withUserActions'

class MyComponent extends React.Component {
  componentDidMount() {
    const { setUser } = this.props

    api.getUser().then(userProfile => setUser(userProfile))
  }

  render() {
    const { logout } = this.props

    return <button onClick={logout}>Logout</button>
  }
}

export default withUserActions(MyComponent)

Edit this example on codesandbox.io

You can share the state the same way

How It Works

Lesley is reactive, so the store reacts when you change the state. When you create Lesley.Store instance via new Store(initialState) Lesley walks through each state property and observes it with getters/setters. If state property value is an object Lesley observes this object too, also if in runtime you assign an object to some of state properties Lesley will also observe it. You can be familliar with this behavior if you have experience in Vue.js

But how Lesley knows which component needs to be updated? When you connect component to the store Lesley maps state to this component via mapState function, remembers which properties component depends on and subscribes component for updates only for this properties, so when you change the state Lesley knows exactly which components should be updated. It makes it fast because of Lesley doesn't make unnecessary iterations or checks to know what to update

Questions

If you have any troubles, questions or proposals you can create the issue
Good pull requests are also appreciated :)

License

MIT

Copyright (c) 2018 - present, Eduard Troshin