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.

Babel constructor only being called once

See original GitHub issue

I have googled everywhere and I cannot find someone with a similar complain regarding React, so I would appreciate it if you take a moment and see if this could be a bug in Storybook. Consider this component:

import React, { Component, PropTypes } from 'react';
import { hh, div, input, pre } from 'react-hyperscript-helpers';

class MailInput extends Component {
  constructor(props) {
    super(props);
    // ----->> ******* THIS IS ONLY CALLED ONCE EVEN THOUGH I HAVE 2 INSTANCES
    alert(JSON.stringify(props));
    // *******  <<<---- 
    this.state = {text: props.text}
  }


  static defaultProps = {
    text: " "
  }

  static propTypes = {
    text: React.PropTypes.string.isRequired
  }

  componentWillReceiveProps(nextProps) {
    this.setState({
      text: nextProps.text
    });
  }

  updateText = (e) => {
    this.setState({
      text: e.target.value,
    });
  }


  render() {

    let stateDebug = pre(["STATE:",JSON.stringify(this.state)]);
    let propsDebug = pre(["PROPS:",JSON.stringify(this.props)]);

    const regularExp =  /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

    let currentText = this.state.text;
    let validText = regularExp.test(currentText);
    return (
      <div>
        <input
          type="email"
          value={currentText}
          onChange={this.updateText} />
        {[stateDebug, propsDebug]}
      </div>
    )
  }
}

export default MailInput;

And this story definition:

import React from 'react';
import MailInput from './component';
import MailInput2 from './component';
import { h } from 'react-hyperscript-helpers';

import { storiesOf, action } from '@kadira/storybook';

storiesOf('MailInput', module)
  .add('valid', () => {
    return h(MailInput,{text: 'diana@kirii.co'})
  })

  .add('not valid', () => {
    return h(MailInput2,{text: '---diana@kirii.co'})
  });

This uses hyperscript (I wanted to check it wasn’t an issue with JSX, but it behaves exactly the same). Basically the alert on the constructor of the component is only triggered once, even though there are two instances of the component, one per story.

If I render in the root element of my app the following component with two instances of MailInput I get expected alert twice:

import React from 'react';
import MailInput from './MailInput/component';
import { h,div } from 'react-hyperscript-helpers';

export default (props) => {
  return div([
    h(MailInput,{text: 'diana@kirii.co'}),
    h(MailInput,{text: '---diana@kirii.co'})
  ]);
}

I have verified that this problem only happens inside storybook, but I cannot explain why. I don’t see how the instantiation of the controller would be affected. Maybe it has something to do with hot-reload?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
arunodacommented, Apr 11, 2016

Check version: v1.10.0. I did a fix for that.

0reactions
davidpelaezcommented, Apr 11, 2016

Works like a charm. Thanks @arunoda

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Cannot use child class' properties within parent ...
So, even when calling a method declared on the parent class, it will inherit the child class' properties. It is just constructor methods...
Read more >
Babel 6 - Mysterious return super() from the constructor. - Reddit
Hey colleagues, I recently noticed that babel is explicitly returing the base class constructor call(super). As far as I know in classes ...
Read more >
What is Babel, and how will it help you write JavaScript?
Any function can be a constructor function, and calling it with the new keyword will ... If we run it through Babel we...
Read more >
Understanding Babel and How it will Help you Write JavaScript
Any function can be a constructor function, and calling it with the new keyword will create a new object. You may learn more...
Read more >
babel/plugin-transform-runtime
A plugin that enables the re-use of Babel's injected helper code to save on ... if (!(instance instanceof Constructor)) { throw new TypeError("Cannot...
Read more >

github_iconTop Related Medium Post

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