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.

`navi-scripts build` fails with react hooks

See original GitHub issue

When running navi-scripts build using hooks it fails with the following error:

Overview

[ohshit] An error occured while building your app
Minified React error #298; visit https://reactjs.org/docs/error-decoder.html?invariant=298 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
Invariant Violation: Minified React error #298; visit https://reactjs.org/docs/error-decoder.html?invariant=298 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.

The unminified error message states:

Hooks can only be called inside the body of a function component.

I know hooks are still in alpha but it might be worth investigating this issue before they release.

Steps to reproduce

  1. Clone the navi repo
  2. cd into navi/examples/create-react-blog
  3. Update react and react-scripts 16.7.0-alpha.2
  4. Replace the App component in App.js to use a hook (see below)
  5. Run yarn build for the example

Hook usage in App.js

export function App(props) {
  const [state] = useState(true)
  return (
    <Nav.Provider navigation={props.navigation}>
      <Nav.Loading>
        {isLoading => (
          <>
            <AppBusyIndicator show={isLoading} />
            <nav>
              <Nav.Link href="/">Home</Nav.Link>
              {state ? 'state is true' : 'state is false'}
            </nav>
            <main>
              <Nav.NotFoundBoundary render={renderNotFound}>
                <Nav.Route />
              </Nav.NotFoundBoundary>
            </main>
          </>
        )}
      </Nav.Loading>
    </Nav.Provider>
  )
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
jamesknelsoncommented, Feb 3, 2019

I’ve just pushed an update which should fix this, and it will be available in Navi 0.11.

The idea is that Navi.app() (now called register()) looks for the “react” and “react-navi” modules, and if it exists, it exports them so that the build script can use them. If they don’t exist, nothing changes, preserving the decoupled design.

Then, navi-scripts hooks into require() and makes it return the instance of “react” and “react-navi” exported by the app bundle. You can override this behavior or add extra shared modules by passing a sharedModules object to register().

Will be publishing the docs and releasing this when React 16.8 is published.

0reactions
joeporpegliacommented, Dec 28, 2018

Most of the solutions I can think of add a lot of coupling between navi and the CRA build process and I really like the layered separation between them right now. Maybe hooks users can pass an async function to app that dynamically imports ReactDOMServer and returns it? That would at least split it out of the production bundle, but the module mapping would still be included in the production webpack manifest/runtime. I’ll keep thinking on it…

Read more comments on GitHub >

github_iconTop Results From Across the Web

build fails even though dev runs fine, complaining about React ...
The reason that it thinks useFunction is a hook is because that's how React's linting checks work. They rely on the use prefix...
Read more >
Understanding common frustrations with React Hooks
React Hooks can be frustrating despite their popularity and widespread use. Learn about some of the drawbacks to using React Hooks.
Read more >
Rules of Hooks - React
Don't call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function, before any...
Read more >
REACT APP Meaning in Japanese - Tr-ex
Examples of using React app in a sentence and their translations · Elements are the smallest building blocks of React apps. · 要素とはReactアプリケーションの最小 ......
Read more >
5 Mistakes to Avoid When Using React Hooks - Dmitri Pavlutin
The hooks executed conditionally can lead to unexpected and hard to debug errors. The way React hooks internally work requires components to ...
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