question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Attach static methods of wrapped component to loadable component

See original GitHub issue

Attach static methods of wrapped component to loadable component

c1.mjs

import * as React from 'react'
const createElement = React.default.createElement
const Component = React.default.Component

export default class c1 extends Component {
  constructor( props ){
    super( props )
  }
  render(){
    return createElement( 'p', {}, 'hello' )
  }
  static async preloadDataState( parameters ){
    debugger
  }
}

main.mjs

import * as React from 'react'
const createElement = React.default.createElement

import * as LoadableComponents from 'loadable-components'
const loadable = LoadableComponents.default.default

import * as LoadableComponentsServer from 'loadable-components/server'
const getLoadableState = LoadableComponentsServer.default.getLoadableState

const c1 = loadable( () => import( /* webpackChunkName: "main.c1" */ './c1.mjs' ), {
  modules: [ './c1.mjs' ],
} )

const composition = createElement( c1 )

getLoadableState( composition ).then( ls => {
  console.log( c1.Component.preloadDataState )
} )

output:

[AsyncFunction: preloadDataState]

Suggestion: attach static methods or fields directly to loadable component ( c1 ): preloadDataState method available by c1.preloadDataState instead of c1.Component.preloadDataState

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
gregbergecommented, Feb 20, 2019

Loadable Components is not design to work with “statical” approach. I recommend you to use “react-navi” for this kind of approach.

0reactions
davidfurlongcommented, Jul 26, 2022

how can i get my static component.serverApiNames in server side.

this seems to be possible now - if you know which Loadable component is loading via a route match (react router or otherwise)

you can do

const loadedComponent = await (component as DefaultImportedComponent<any>).load();
loadedComponent?.default?.accessYourStaticPropName
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using React Loadable for Code Splitting by Components and ...
We pass a function as a value to loader which returns the Hello component we created earlier, and we make use of import()...
Read more >
Loadable Components
This is a function that receives the imported module (what the import() call resolves to) and the props, and returns the component.
Read more >
Why when a component is wrapped inside a High Order ...
The reason static values aren't available when using a higher order component (for example, enhance in the example below) is because the ...
Read more >
How To Create Wrapper Components in React with Props
You'll learn to split data and refactor components as you create flexible wrapping components. By the end of this tutorial, you'll have a ......
Read more >
Introduction to Higher-Order Components in React by example
Don't Use HOCs inside the render method; Static methods must be copied ... Rather, a HOC composes the original component by wrapping it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found