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.

Object(...) is not a function - Named exported HOC's inside a component file and used on a child component of its component break bundle on runtime [ Repo example inside ]

See original GitHub issue

Introduction

First of all I have to say that I am really concerned about its not a good practice to create a HOC on a component file and name export it but…

I’ve created a repository with a real case scenario isolated and so so simplified: https://github.com/carlesnunez/webpack-hoc-from-child-error

  • master branch is the branch with the bug
  • master-fixed branch is the branch with the ‘patch’

The problem

In the example branch master you’ll see that I’m using a HOC created on component PARENT app.js exported as a named export and consumed on the componentConsumingHOC file.

Doing this and bundling everything to production code does the next runtime error:

Uncaught TypeError: Object(...) is not a function

If we check the code we’ll see the code of this ‘strange’ error:

captura de pantalla 2019-01-30 a las 15 38 17

Nothing weird right? But it seem that the ‘Object’ that can use is our myHOC function.

If we step further into the error checking it with devtool: ‘none’ and mode:‘production’ we will see the next bundled code:

captura de pantalla 2019-01-30 a las 15 37 02

When s seems to be our HOC

To check it run:

> npm run build

And use a the dev server that you prefeer I’ve used:

> cd build && python -m SimpleHTTPServer 8082

Go to http://localhost:8082 and look at the console.

The ‘patch’

Don’t declare High order components that will be consumed by childrens on it’s parent component files.

To check it run:

> git checkout master-fix

> npm run build

And use a the dev server that you prefeer I’ve used:

> cd build && python -m SimpleHTTPServer 8082

Conclusions

I am really concerned about its not a good practice to create a HOC on a component file and name export it but…

When I create a HOC on a parent component export it as a named export and consume it from a child component code breaks on runtime.

I’m not really sure if is a webpack problem, babel… Seems that is trying to use a module before Its declared or maybe it can’t even reach which module it is on runtime and it just throw the error.

What could be happening? Is this a bug? Hope it helps others

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
jurecommented, Feb 26, 2019

I’ve faced a similar issue (worked fine in dev, but had the Object(...) is not a function issue in production builds) and disabling the concatenateModules optimization resolved it.

The component in question was a HOC that used recompose and styled-components. 🤷‍♂️

If I can get some tips on what to look at/for, I’d be happy to dig a bit deeper.

1reaction
sokracommented, Jan 31, 2019

Yeah I also think this is working as expected as it’s a circular dependencies. You can probably fix it this way:

- export const myHOC = (Component) => {
+ export function myHOC(Component) {
Read more comments on GitHub >

github_iconTop Results From Across the Web

HOC import TypeError: Object(...) is not a function
This seems to be a problem with hoisting of 'exports'. From what I can see, the imports get hoisted, but I could not...
Read more >
Invalid Hook Call Warning - React
You are probably here because you got the following error message: Hooks can only be called inside the body of a function component....
Read more >
React SDK - Split Help Center
// The npm package exposes the different components and functions of the library as named exports. // If you were using the bundle...
Read more >
Releases - styled-components
Releases. Updating styled components is usually as simple as npm install . Only major versions have the potential to introduce breaking changes (noted...
Read more >
Learning the Basics - Gradle User Manual
In Gradle, the scope of a dependency is called a configuration. ... Software projects often break up software components into modules to improve ......
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