New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

react-lazy-value

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-lazy-value

Lazily evaluate a value during the render phase of your component.

latest
Source
npmnpm
Version
0.7.0
Version published
Maintainers
1
Created
Source

react-lazy-value

Lazily evaluate a value during the render phase of your component.

react-lazy-value can be used for data fetching, code splitting, state management, and other async use cases.

Install

npm

npm install react-lazy-value react react-dom

yarn

yarn add react-lazy-value react react-dom

Examples

Data Fetching

import React from "react";
import ReactDOM from "react-dom";
import lazyValue from "react-lazy-value";

const currentUser = lazyValue(async () => {
  const response = await fetch(`/api/me`);
  const json = await response.json();
  return json;
});

function Loading() {
  return <h1>Loading...</h1>;
}

function App() {
  return <h1>Hello {currentUser.value.name}!</h1>;
}

ReactDOM.render(
  <React.Suspense fallback={<Loading />}>
    <App />
  </React.Suspense>,
  document.getElementById("app")
);

Code Splitting

import React from "react";
import ReactDOM from "react-dom";
import lazyValue from "react-lazy-value";

const moment = lazyValue(() => import("moment"));

function Loading(props) {
  return <h1>Loading...</h1>;
}

function App() {
  return <h1>{moment.value.calendar()}!</h1>;
}

ReactDOM.render(
  <React.Suspense fallback={<Loading />}>
    <App />
  </React.Suspense>,
  document.getElementById("app")
);

Reducer

import lazyValue from "react-lazy-value";

function reducer(state, action) {
  switch (action.type) {
    case "LOAD_USER":
      return lazyValue(async () => {
        const response = await fetch(`/api/users/${action.payload.id}`);
        const json = await response.json();
        return json;
      });

    case "UPDATE_USER":
      return lazyValue(() => ({
        ...state.value,
        ...action.payload
      }));

    default:
      return state;
  }
}

Eager Initialization

import lazyValue from "react-lazy-value";

const currentUser = lazyValue.touch(
  lazyValue(async () => {
    const response = await fetch(`/api/me`);
    const json = await response.json();
    return json;
  })
);

How does it work?

Async

react-lazy-value relies on React Suspense when evaluating a promise (or thenable). Suspense lets components “wait” for something before rendering.

JSON.stringify

The lazy object is treated as undefined when in a pending or error state.

Keywords

react

FAQs

Package last updated on 27 Dec 2018

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