Giter VIP home page Giter VIP logo

react-loadable's Introduction

A higher order component for loading components with dynamic imports.Support vite and webpack.

Install

yarn add @caixue/react-loadable or npm install @caixue/react-loadable

Usage

import Loadable from '@caixue/react-loadable';
import Loading from './my-loading-component';

const LoadableComponent = Loadable({
  loader: () => import('./my-component1'),
  loading: Loading,//your personal loading conpontent
  timeout: 20000,//timeout(ms) default 10000
});//please save a LoadableComponent at module top level

//or
const LoadableComponent = Loadable(() => import('./my-component1'));
 
export default class App extends React.Component {
  render() {
    return <LoadableComponent/>;
  }
}

for get component ref

  • use getRef can get component ref.
//app.tsx
import Loadable from '@caixue/react-loadable';
import React, { useEffect, useRef } from 'react';
const MyInput = Loadable(() => import('./myInput'));

const App: React.FC = () => {
    const myInput = useRef() as React.MutableRefObject<React.ElementRef<typeof MyInput>>;
    useEffect(() => {
        myInput.current.getRef().then((input) => {
            input.current.focus();
        })
    })
    return <MyInput ref={myInput} />
}

export default App;

//myInput.tsx
import React, { forwardRef, useRef, useImperativeHandle } from 'react';

const MyInput = forwardRef((props, ref) => {
    const input = useRef<HTMLInputElement | null>();
    useImperativeHandle(ref, () => {
        return input;
    })
    return <input ref={(el) => { input.current = el }} />
})

export default MyInput;

react-loadable's People

Contributors

cx690 avatar

Stargazers

Alex Strand avatar Zickie Loox avatar Ahmet Simsek avatar  avatar Zoltan Erdos avatar HeXiaoliang avatar itmanyong avatar

Watchers

James Cloos avatar  avatar

react-loadable's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.