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

@mile-hi-labs/react-data

v3.7.4

Published

A state management library for React and React Native applications.

Downloads

3

Readme

Overview

React Data is a state management library for React and React Native applications. The library is lightweight and configurable so you can quickly connect React Data to your API and then grow with it over time.


How it Works

React Data uses the Context Hooks to construct a global data store that connects the Adapters, Serializers, and Models in your project to communicate with your API and store local data. This way, you can access data fetched via previous network calls and manage that data via the local store. As an alternative, you can also bypass the local store and simply use React Data to fetch data from your API and make it available in your component. The choice is yours. React Data is designed to work with a JSON-API and REST patterns though you're welcome to override / configure the BaseAdapter and BaseSerializer to match your needs. To learn more about React Data, please visit the API Documentation.


Quick Start

Install

npm install @mile-hi-labs/react-data

Store Provider

Add the following to your app.jsx file or near the top of your application.

# app.jsx

import React from 'react';
import { Store, StoreProvider } from '@mile-hi-labs/react-data';
import * as Adapters from 'adapters';
import * as Models from 'models';
import * as Serializers from 'serializers';
import Router from 'router';

const apiDomain = 'https://library-api.milehilabs.dev';
const store = new Store({ apiDomain: apiDomain, adapters: Adapters, serializers: Serializers, models: Models });

const App = (props) => {

  return (
    <StoreProvider context={store}>
    	<Router />
  	</StoreContext>
	)
}

export default App;

Store Consumer

Then, you can access the store from any route or component like so:

# routes/mkt/index.jsx

import React, { useEffect } from 'react';
import { withStore } from '@mile-hi-labs/react-data';
import UserList from 'components/user/user-list';

const MktIndex = (props) => {
	const { store } = props;
	const [ users, setUsers ] = useState([]);
	const [ loading, setLoading ] = useState([]);

	// Hooks
	useEffect(() => {
		fetchData()
	}, []);


	// Methods
	const fetchData = async () => {
		try {
			setLoading(true);
			let storeRecords = store.findAll('user', {} );
			setUsers(storeRecords);
		} catch(e) {
			console.log(e);
		} finally {
			setLoading(false);
		}
	}

	// Render
	return (
		<div className='container'>
			{loading ? (
				<h6>...loading...</h6>
			) : (
				<UserList users={users}/>
			)}
		</div>
	)
}

export default withStore(MktIndex);

Core Components

While React Data is designed to work with minimal configuration, you'll likely end up writing your own Adapters, Serializers, and Models to construct your local data store and customize network calls. As such, it's important to understand the core components of this library, how they interconnect, and how to configure them.

The Store

The Store acts as the central hub connecting your adapters, serializers, and models together to communiciate with your API, retrieve and store data locally. So, while you'll need to author those components elsewhere (hopefully in their respective directories) you'll access them from any component through the store prop as shown in the Quickstart guide.

Adapters

Adapters handle URL configuration when communicating with your API. This way, we're able to construct the base URL, endpoint, and any parameters you're looking to pass into the server seamlessly. When authoring an Adapter, you should inherit from the BaseAdapter provided by React Data to ensure access to the core APIs. Then, you can override any method

# adapters/blog.jsx

import { BaseAdapter } from '@mile-hi-labs/react-data';


class BlogAdapter extends BaseAdapter {
	static blogApi: 'https://blog-api.yourdomain.com';

	static urlForQueryRecord(modelName, id) {
		let url = super.urlForQueryRecord(modelName, id);

		if (this.blogApi) {
			return `${this.apiDomain}/posts/{id}`;
		}

		return url;
	}
}

export default BlogAdapter;

Models

Models declare the data you expect to send to / receive from the server and then how it should populate your local data store. We recommend keeping your models lightweight and then remove them when they're done being used. This way, you maintain a performant store and application. When authoring a model, you should inherit from the BaseModel provided by React Data like so:

# models/user.jsx

import { BaseModel } from '@mile-hi-labs/react-data';


class UserModel extends BaseModel {
	constructor(type, props = {}) {
		super(type, props);

		this.firstName = props.firstName;
		this.lasttName = props.lastName;
	}


	// Computed
	get fullName() {
		return this.firstName + ' ' + this.lastName;
	}
}

export default UserModel;

Serializers

Serializers handle data serialization and normalization when passing data to and from your server. This way, we're able to format your requests appropriately while giving you the opportunity to customize your data as it comes in (or goes out) as you see fit. When authoring a serializer, you should inherit from the JsonApiSerializer provided by React Data like so:

# serializers/user.jsx
import { JsonApiSerializer } from '@mile-hi-labs/react-data';

class UserSerializer extends JsonApiSerializer {
	// Overrides
	static attrs = {
		fullName: { serialize: false }
	}


	static serializeAttr(data, key) {
		let formattedAttr = super.serializeAttr(data, key);
		if (key == 'age') {
			return parseInt(data[key])
		}
		return formattedAttr;
	}
}

export default UserSerializer;

Demo

This project comes with a built-in React and React Native demo.

Development

This projects uses Webpack to build the project. Please see package.json for available scripts.

Web

  • Clone this repository
  • Run npm link from this library
  • Open up the consuming project / demo project and run npm link @mile-hi-labs/react-data
  • Run npm run build to push code from the library to your project

Native

React Native's metro bundler doesn't accept the steps below. Please contact the project author for an alternative path.

  • Clone this repository
  • Run npm install from this library
  • Run npm run build from this library
  • Open up the consuming project / demo project and run npm install path/to/this/project
  • Repeat the steps above to consume the latest code

Publish

  • Commit changes
  • Run npm version patch (or minor / major) to bump the version, build, deploy, and commit the new version. https://docs.npmjs.com/cli/v6/commands/npm-version

Links