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.

build-storybook differences?

See original GitHub issue

I’m trying to figure out a weird edge case I’m having, using mobx for store management inside stories.

Take this (highly convoluted) story:

import React from 'react';
import { observable } from 'mobx';
import { observer } from 'mobx-react';

// Create a new store
const store = observable({
  toggle: true,
});

// Toggle the value inside the store
function toggle() {
  store.toggle = !store.toggle;
}

// Test component -- uses the store above, but NOT a direct observer
const Headline = props => (
  <h1>{props.store.toggle.toString()}</h1>
);

Headline.propTypes = {
  store: React.PropTypes.shape({
    toggle: React.PropTypes.bool.isRequired,
  }),
};

// Generator func to return x number of <Headline> components, fed the store
function* getComponents(count) {
  for (let i = 1; i <= count; i++) {
    yield (
      <Headline key={i} store={store} />
    );
  }
}

// Wrap THIS one in an observer, and go down the chain
const Toggle = observer(() => (
  <div>
    {[...getComponents(5)]}
    <button
      onClick={toggle}>Toggle</button>
  </div>
));

// Export as an instantiated JSX comp.
export default <Toggle />;

When running in dev (npm run dev), clicking ‘toggle’ works fine…

works

However, when running in production (npm run build-storybook and starting a local server), toggle suddenly stops working…

not-working

My gut reaction is that it has something to do with transpilation differences. Stores built using MobX’s observer function work differently depending on whether there’s a prototype chain (if there is, properties on the object are observable, but not the parent object itself). But that doesn’t seem to be happening because in both cases, there’s no prototype chain, and in other examples where the parent store is not fed in but used directly, it works as expected.

It seems the observer chain is ‘lost’ somehow when the generator passes it to it.

I know it’s a totally weird edge case, but are there any known differences between how code is rendered in dev vs. bundling?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
leebensoncommented, Oct 18, 2016

d’oh, it suddenly makes sense.

.propTypes isn’t evaluated in production (it’s a dev-only check, right?) So it wouldn’t ‘observe’ the underlying value, and thus wouldn’t re-calculate.

So, in this case, .propTypes was causing a side-effect that unintentionally made the component appear to be observing (I guess inside the original call stack passing the prop?), but breaks as soon as .propTypes is dropped in production – which is what it should do, because the final component isn’t an observer.

Makes sense now!

1reaction
leebensoncommented, Oct 18, 2016

Something else that is a bit freaky too (maybe related to mobxjs/mobx-react#56)

Under npm run storybook, dropping .propTypes actually prevents it from observing:

const Headline = props => (
  <h1>{props.store.toggle.toString()}</h1>
);

/* commenting out .propTypes *stops* it working in dev */
// Headline.propTypes = {
//   store: React.PropTypes.shape({
//     toggle: React.PropTypes.bool.isRequired,
//   }),
// };

So I guess that’s exactly what mobxjs/mobx-react#56 relates to – adding a .propTypes definition causes a re-render (but only in dev; not in production), thus making the component update via npm run storybook but NOT npm build-storybook.

interesting!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Publish Storybook
Storybook is a frontend workshop for building UI components and pages in isolation. Thousands of teams use it for UI development, testing, and...
Read more >
docs" and "build-storybook"? · Discussion #17385 - GitHub
what's the difference between "build-storybook --docs" and "build-storybook"? #17385. Unanswered. LastStranger asked this question in General.
Read more >
How is bit.dev different from Storybook? | by Jonathan Saring
The fundamental difference is very simple to explain: StoryBook is for visually developing and documenting UI components. Bit is for building ...
Read more >
React Storybook vs Storybook | What are the differences?
This functionality allows you to develop UI components rapidly without worrying about the app. It will improve your team's collaboration and feedback loop; ......
Read more >
Storybook vs Styleguidist - Chromatic
A comparison of the top UI component explorers. ... In practice, teams use Storybook to start building UI components in isolation and reason ......
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