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.

Nav not rendered correctly in ParcelJs build: NavbarContext is removed by Nav.Collapse

See original GitHub issue

Describe the bug

The Nav component is not rendered correctly in the dist output of a ParcelJs project.

To Reproduce

  1. Create a ParcelJs project with React, as described at https://blog.jakoblind.no/react-parcel/.
  2. Create the following App.jsx:
import React from "react";
import Nav from "react-bootstrap/Nav";
import Navbar from "react-bootstrap/Navbar";
import NavbarContext from "react-bootstrap/NavbarContext";

const App = () => (
  <Navbar bg="primary">
    <Navbar.Toggle />
    <NavbarContext.Consumer>
      {value => console.log("NavbarContext outside Collapse", value)}
    </NavbarContext.Consumer>
    <Navbar.Collapse>
      <NavbarContext.Consumer>
        {value => console.log("NavbarContext inside Collapse", value)}
      </NavbarContext.Consumer>
      <Nav>
        <Nav.Link href="/foo">
          Foo
        </Nav.Link>
        <Nav.Link href="/bar">
          Bar
        </Nav.Link>
      </Nav>
    </Navbar.Collapse>
  </Navbar>
);

export default App;

Reproducible Example

I pushed a minimal ParcelJs project to demonstrate the behavior. Build steps:

git clone https://github.com/mdenburger/react-bootstrap-nav-issue
cd react-bootstrap-nav-issue
yarn build

And open dist/index.html in a browser.

Expected behavior

The navbar should render correctly, but it doesn’t: the example links Foo and Bar are not visible because the CSS classes of div rendered by the Nav component are not correct.

  • Expected: class navbar-nav
  • Actual: class nav

Cause: the Nav component cannot find the NavbarContext. It seems that the Nav.Collapse component somehow sets the context to null, as demonstrated by the console.log statements. Outside Nav.Collapse the context is correct, but inside Nav.Collapse it’s null. As a result, the Nav component does not realize it’s inside a navbar, and does not render the right classes.

Everything does work correctly in ParcelJs’ development server (started with yarn dev in the example repo). So it seems to be the result of ParcelJs’ bundling. Yet I haven’t seen this behavior in any other usages of the Context API in my ParcelJs project.

Environment

  • Operating System: Ubuntu
  • Browser: Chrome 80
  • React-Bootstrap Version: 1.0.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jonsacommented, May 13, 2020

@kosmiq Would be possible to make a PR with the following change? It seems to fix the problem for me.

import React from 'react';
var context = React.createContext(null);
context.displayName = 'AccordionContext';
export default context;
import React from 'react';
var context = React.createContext(null);
context.displayName = 'CardContext';
export default context
import React from 'react';
var context = React.createContext(null);
context.displayName = 'NavbarContext';
export default context;
0reactions
jquensecommented, May 14, 2020

yeah lets do it

Read more comments on GitHub >

github_iconTop Results From Across the Web

Browse Javascript Technical Problem Clusters - Code Grepper
src/components/_App/Navbar.js Module not found: Can't resolve '../. ... Jquery Scroll on div using anchor tag is not Working properly · if class is...
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