Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

lazy react

import React, { Suspense } from 'react';

const OtherComponent = React.lazy(() => import('./OtherComponent'));

function MyComponent() {
  return (
    <div>
      <Suspense fallback={<div>Loading...</div>}>
        <OtherComponent />
      </Suspense>
    </div>
  );
}
Comment

react.lazy

import React, { Suspense } from 'react';
import Tabs from './Tabs';
import Glimmer from './Glimmer';

const Comments = React.lazy(() => import('./Comments'));
const Photos = React.lazy(() => import('./Photos'));

function MyComponent() {
  const [tab, setTab] = React.useState('photos');
  
  function handleTabSelect(tab) {
    setTab(tab);
  };

  return (
    <div>
      <Tabs onTabSelect={handleTabSelect} />
      <Suspense fallback={<Glimmer />}>
        {tab === 'photos' ? <Photos /> : <Comments />}
      </Suspense>
    </div>
  );
}

/*
In this example, if tab gets changed from 'photos' to 'comments', 
  but Comments suspends, the user will see a glimmer. 
  This makes sense because the user no longer wants to see Photos, 
    the Comments component is not ready to render anything, 
      and React needs to keep the user experience consistent, 
        so it has no choice but to show the Glimmer above.

However, sometimes this user experience is not desirable. 
In particular, it is sometimes better to show the “old” UI while 
  the new UI is being prepared. You can use the new startTransition 
API to make React do this:
*/

function handleTabSelect(tab) {
  startTransition(() => {
    setTab(tab);
  });
}
Comment

lazy react

import("./math").then(math => {
  console.log(math.add(16, 26));
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: usecontext hook 
Javascript :: export json to excel in javascript 
Javascript :: mdn trim 
Javascript :: ordenar numeros array javascript 
Javascript :: javascript for in 
Javascript :: You need to inject a global window.jQuery first. 
Javascript :: javascript integer to binary 
Javascript :: open modal on clicking select option in react 
Javascript :: how to append item to an array in foreach javascript 
Javascript :: moment iso string 
Javascript :: javascript regex match sequence 
Javascript :: react native update state array of objects 
Javascript :: alternative to setinterval 
Javascript :: 10 javascript interview questions 
Javascript :: sort array based on multiple columns javascript 
Javascript :: jquery camera priview 
Javascript :: how to export fs.readFile 
Javascript :: moment js 
Javascript :: nodejs request post 
Javascript :: TypeError: this.setState is not a function 
Javascript :: how to display words from an array in a box in javascript 
Javascript :: open file method in node js 
Javascript :: use cors 
Javascript :: google translate javascript 
Javascript :: js remove all children 
Javascript :: javascript get the screen color depth 
Javascript :: react doc viewer 
Javascript :: js how to get element csswidth 
Javascript :: angular 8 remove cookies 
Javascript :: return jsx in for loop 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =