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

if more the one same async components are rendered in same page, only one will be rendered #100

Open
shuijingyue opened this issue Nov 27, 2019 · 0 comments

Comments

@shuijingyue
Copy link

comp.js

import React, {Component} from "react";

class Comp extends Component{
  render() {
    return (
      <div>async component</div>
    )
  }
}

export default Comp;

index.js

import { render } from 'react-dom';
import React, { Component } from 'react';
import { asyncComponent } from "react-async-component";
const Comp = asyncComponent({
  resolve: () => new Promise(resolve =>
    require.ensure(
      [],
      (require) => {
        resolve(require('./components/comp'));
      }
    )
  )
})
// App component will render Comp after 5 seconds
export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      needComp: false
    }
  } 
  componentDidMount() {
    setTimeout(() => {
      this.setState({needComp: true})
    }, 5 * 1000)
  }
  handleClick = () => {
    this.setState({needComp: true});
  }
  render() {
    return (
      <>
        <button onClick={()=> {console.log(this.state.needComp)}}>log</button>
        <button onClick={this.handleClick}>load comp</button>
        {this.state.needComp ? <Comp/> : "loding"}
      </>
    );
  }
}
// the page should render two Comps after 5 seconds, but only one shows
// another Comp doesn't  show until you click another App's 'load comp' button
render(<><App/><App/></>, document.getElementById('root'));
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

No branches or pull requests

1 participant