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

@morningtrain/react-resources

v7.9.0

Published

## Concepts The following section describes the different concepts introduces in this package.

Downloads

29

Readme

React Resources

Concepts

The following section describes the different concepts introduces in this package.

Mentioned features are not exhaustive but will highlight core features. Extra features not mentioned here are mainly used internally by the package or are outdated.

Model

A model is at its core a JavaScript Map that is made observable using Mobx. It represents an object containing data that can be used throughout the React application.

Models are mostly used to represent a single (database) entry retrieved through an API. It can however be anything.

This package provides the following related features:

Model

The Model component will execute and API request to the operation matching the props and will provide a Model object with the results to any child components.

import React from 'react'
import { Model } from '@morningtrain/react-resources'

export default function MyComponent() {
  return (
    <Model namespace={'api'} resourceName={'users'} operationName={'read'}>
      {/* Components that will read from model */}
    </Model>
  )
}

ModelWithForm

This component extends the Model component but adds a form element.

The form element will submit to and refresh model through the specified API endpoints when paired with a model.

useModel

A hook to retrieve the nearest model. It will be the model provided through the ModelContext.

import React from 'react'
import { useModel } from '@morningtrain/react-resources'

export default function MyComponent() {
  const model = useModel()
  
  /// Any other logic in MyComponent
}

useValue

A hook to retrieve a value from the nearest model. The path provided to the hook supports dot notation for getting nested values.

Any updates to the value in the model will trigger a rerender of the component with the updated value.

import React from 'react'
import { useValue } from '@morningtrain/react-resources'

export default function MyComponent() {
  const value = useValue('path.to.value')
  
  /// Any other logic in MyComponent
}

WhenModel

Conditional component to only render child components when a model is provided and when the provided model has data.

WhenModelValue

Conditional component to only render child components when a value in the model matches certain conditions.

It takes 3 props:

name - Path to value in the model. Dot-notation is supported to get nested values.

condition - It will try to match the value to this prop directly. A function can be passed which will get the current value as a parameter. If the prop is not provided, then it will check if the value is neither null nor undefined.

negate - Boolean to reverse the conditional. Defaults to false.

Collection

A collection is an observable mobx array of models.

This package provides the following related features:

Collection

This is the primary component for calling an API endpoint and providing the resulting array as a collection instance to any child components.

import React from 'react'
import { Collection } from '@morningtrain/react-resources'

export default function MyComponent() {
  return (
    <Collection namespace={'api'} resourceName={'users'} operationName={'index'}>
      {/* Components that will read from collection */}
    </Collection>
  )
}

useCollection

A hook to retrieve the nearest collection. It will be the collection provided through the CollectionContext.

import React from 'react'
import { useCollection } from '@morningtrain/react-resources'

export default function MyComponent() {
  const collection = useCollection()
  
  /// Any other logic in MyComponent
}

ForEachModel

The ForEachModel component will loop over the collection and provide a model for each entry.

Any children provided to the component will be rendered for each entry.

import React from 'react'
import { Collection, ForEachModel } from '@morningtrain/react-resources'

export default function MyComponent() {
  return (
    <Collection namespace={'api'} resourceName={'users'} operationName={'index'}>
      <ForEachModel>
        {/* Components that will read from the model */}
      </ForEachModel>
    </Collection>
  )
}

ValueToCollection

WhenCollectionIsEmpty

A conditional component that will render its children when the closest collection instance does not have any entries.

This component can be used to display a fallback component when the collection is missing entries.

WhenCollectionIsNotEmpty

A conditional component that will render its children when the closest collection instance do have entries.

This component can be used to only start displaying the UI when the Collection has been created.

Selection

A selection is a class that keeps track of which items in a given collection has currently been selected.

These features are mostly used to support multi-select in tables by checking checkboxes in a table. The concepts are however abstract enough to be used for other scenarios where something from a list of items needs to be selected.

This package provides the following related features:

Selection

This is the primary component for creating and providing a Selection instance to child components.

The selection needs to be provided with a collection instance to work. It can either be provided manually using CollectionContext or through Collection.

import React from 'react'
import { Collection, Selection } from '@morningtrain/react-resources'

export default function MyComponent() {
  return (
    <Collection namespace={'api'} resourceName={'users'} operationName={'index'} >
      <Selection>
        {/* Components using the selection */}
      </Selection>
    </Collection>
  )
}

The selection will use id as the key to get the unique identifier of the model entry. When using a model with a different key, the modelKeyName prop should be supplied to the selection.

SelectOnPipe

This component is a pipe (see pipeline docs) to select an entry when piping.

Multi-select is enabled by default. This can be turned off by setting the multiple prop to false. When multi-select is not enabled, then the selection will be cleared upon selecting something new.

The SelectOnPipe component needs to be placed inside a model for it to figure out which items to select.

It could be used together with an OnClickPipeline to select collection items on click events.

ClearSelectionOnPipe

This component is a pipe (see pipeline docs) to clear (reset) the selection object when piping.

ClearMissingSelectionOnExecute

This component will react to any changes to the provided collection and validate the currently selected items.

If any of the currently selected items are missing from the collection, then these entries will be unselected.

This functionality can be used to clear selected items when paginating or filtering the collection.

SelectedToCollection

When placed inside a selection, it will provide a new collection to its children that is a copy of the original collection filtered to the currently selected items.