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

mobx-select

v2.3.1

Published

MobX and MobX-State-Tree store selector for using with mobx-react-lite

Downloads

46

Readme

mobx-select

MobX and MobX-State-Tree store selectors (like Redux mapStateToProps and mapDispatchToProps) for using with mobx-react-lite

NPM version

Lib has no deps and tiny. Its size limited to 814 bytes (minified and gzipped).

How to use

yarn add mobx-select

asset.js

import {select} from 'mobx-select'

function getAsset(app, props) {
  const asset = app.assets.get(props.id)
  return {asset}
}

function onRemoveAsset(app, prop) {
  const {id} = props
  
  const onRemove = useCallback(() => {
    app.removeAsset(id)
  }, [id])

  return {onRemove}
}

export default select(
  Asset,
  getAsset,
  onRemoveAsset,
  // ...other
)

function Asset({
  asset = {},
  onRemove,
}) {
  return (
    <div>
      <span>{asset.name}</span>
      <button onClick={onRemove}>Remove</button>
    </div>
  )
}

app.js

import {StoreProvider} from 'mobx-select'
import Asset from './asset'

// create MobX or MobX-State-Tree store
const app = createStore()

export default function App() {
  return (
    <StoreProvider value={app}>
      <Asset id='1'/>
    </StoreProvider>
  )
}

Render optimization

If you access nested observable values (MobX objects, maps, arrays) only inside selectors but not in component you can use memo or PureComponent to prevent unnecessary extra renderings.

import {memo} from 'react'

function getAssetTitle(app, props) {
  const title = app.assets.get(props.id)?.title
  return {title}
}

export default select(
  memo(Title),
  getAssetTitle,
)

function Title({
  title,
}) {
  return (
    <span>{title}</span>
  )
}

Using with non-function components

You can use select() with non-function components. But keep in mind that access to all nested observable values (MobX objects, maps, arrays) inside component will not be tracked. So you need to get all necessary fields in selector and pass it as (!) scalar values.

Use warnNonFunction: false option to hide warning about it. Warning for memo and PureComponent will be omitted automatically (assumed that you know what you're doing).

function getUserId(app) {
  const userId = app.userId
  return {userId}
}

export default select(
  Title,
  getAsset,
  getUserId,
{warnNonFunction: false})

class Title extends React.Component {
  render() {
    const {userId, asset} = this.props

    // (!) WARN: access to price will not be tracked!
    // (!) changing its value will not trigger component re-render 
    const price = asset?.price

    return (
      <span>user={userId} {price}</span>
    )
  }
}

References

References will be automatically passed to components:

function UserName({
  name,
}, ref) {
  return (
    <input ref={ref} value={name}/>
  )
}

class UserSignInCount extends React.Component {
  render() {
    return (
      <span>{this.props.count}</span>
    )
  }
  
  log() {
    console.log(this.props.count)
  }
}

function App() {
  const nameRef = useRef()
  const signInCountRef = useRef()
  
  const tadam = useCallback(() => {
    nameRef.current.value = "User TADAM!"
    signInCountRef.current.log()
  })
  
  return (
    <>
      <UserName ref={nameRef}/>
      <UserSignInCount ref={signInCountRef}/>
      <button onClick={tadam}>TADAM!</button>
    </>
  )
}