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

fix(Modal): handle init modal in SSR #1495

Merged
merged 3 commits into from May 21, 2019

Conversation

lh0x00
Copy link
Contributor

@lh0x00 lh0x00 commented May 20, 2019

  • Bug fix
  • New feature
  • Chore
  • Breaking change
  • There is an open issue which this change addresses
  • I have read the CONTRIBUTING document.
  • My commits follow the Git Commit Guidelines
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
    • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

In server-side rendering, Modal will call init but in this time not found document and it's will breaks.

Copy link
Collaborator

@virgofx virgofx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The defined isSSR flag isn't reliable and window is still defined in multiple SSR environments for polyfills and backwards compatibility.

I believe you can drop that completely and just move this.init() from construction into componentDidMount() as this should work for all cases since it's not called in SSR render. @lamhieu-vk: Can you make this change and confirm this works in your testing for both SSR and client side rendering with open={true} specified.

Reference:

Fix: #1071

@virgofx
Copy link
Collaborator

virgofx commented May 21, 2019

Changes look good. @lamhieu-vk Is everything working as expected in your environment?

@lh0x00
Copy link
Contributor Author

lh0x00 commented May 21, 2019

@virgofx Sorry, I forget change title to WIP. I will mention to you after done!

@lh0x00 lh0x00 changed the title fix(Modal): handle init modal in SSR WIP - fix(Modal): handle init modal in SSR May 21, 2019
@virgofx
Copy link
Collaborator

virgofx commented May 21, 2019

No problem. Just let me know if everything is working in your environment as expected as I have not tested personally

@lh0x00 lh0x00 changed the title WIP - fix(Modal): handle init modal in SSR fix(Modal): handle init modal in SSR May 21, 2019
@lh0x00
Copy link
Contributor Author

lh0x00 commented May 21, 2019

@virgofx I updated PR, I tested in my app and use next.js to SSR. PR's ready to merge!

@virgofx
Copy link
Collaborator

virgofx commented May 21, 2019

Thanks. Looks good

/cc @TheSharpieOne

@TheSharpieOne
Copy link
Member

@virgofx you can squash and merge 😉

@TheSharpieOne TheSharpieOne merged commit c844ab1 into reactstrap:master May 21, 2019
@virgofx
Copy link
Collaborator

virgofx commented May 21, 2019

Just wanted you to look at it :)

@lh0x00
Copy link
Contributor Author

lh0x00 commented May 22, 2019

Many thanks for @virgofx @TheSharpieOne

@justingrant
Copy link

justingrant commented May 22, 2019

BTW, this PR also fixes #1323.

@justingrant
Copy link

@lamhieu-vk - Does your PR break auto-focus behavior? The implementation of componentDidMount checks this.state.isOpen but your PR moved the initialization of state.isOpen into a setState call in componentDidMount. Because setState runs asynchronously (at least in browser React, although I'm not sure about SSR), state.isOpen will always be false when componentDidMount runs, so the auto-focus check if (this.state.isOpen && this.props.autoFocus) will never be true.

Shouldn't that condition be changed to use this.props instead of this.state? Like this:

  componentDidMount() {
    if (this.props.isOpen) {
      this.init();
      this.setState({ isOpen: true })
    }

    if (this.props.onEnter) {
      this.props.onEnter();
    }

    // currently:  if (this.state.isOpen && this.props.autoFocus) {
    if (this.props.isOpen && this.props.autoFocus) {
      this.setFocus();
    }

    this._isMounted = true;
  }

@lh0x00
Copy link
Contributor Author

lh0x00 commented May 29, 2019

@justingrant I think your comment is true, and it's a break in this case. I will update it soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants