Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Babel constructor only being called once #81

Closed
davidpelaez opened this issue Apr 8, 2016 · 7 comments
Closed

Babel constructor only being called once #81

davidpelaez opened this issue Apr 8, 2016 · 7 comments

Comments

@davidpelaez
Copy link

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?

@davidpelaez
Copy link
Author

As a result of this problem, the state of the second instance is for some reason whatever the initial state was on the first one.

@bhishp
Copy link

bhishp commented Apr 11, 2016

+1. I find that I am unable to utilise defaultProps when creating multiple stories for a given component

@arunoda arunoda added the bug label Apr 11, 2016
@arunoda
Copy link
Member

arunoda commented Apr 11, 2016

I think I know the issue.
We need to unmount the existing component in the DOM, before rendering the new one.

See: ReactDOM.unmountComponentAtNode()

@arunoda
Copy link
Member

arunoda commented Apr 11, 2016

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

@bhishp
Copy link

bhishp commented Apr 11, 2016

Yeah, works for me. Cheers

@arunoda
Copy link
Member

arunoda commented Apr 11, 2016

Great. Closing this then.

@arunoda arunoda closed this as completed Apr 11, 2016
@davidpelaez
Copy link
Author

Works like a charm. Thanks @arunoda

ndelangen pushed a commit that referenced this issue Feb 23, 2024
Make selectors Storybook 8 compatible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants