0

I read this Reactjs async rendering of components and tried it to my own code but, error :

Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.

I have no clue why it doesn't work. please help me!

 constructor(props) {
    super(props);
    this.state = { asyncComponent: null };
  }

public componentDidMount() {
    import(`../../i18n/locales/${this.props.lang}`).then(o => {
      this.setState({
        asyncComponent: (
          <CustomSelects
            options={o.option}
            formattedMessageId="createroom_genre"
            value={this.props.genre}
            handleValueChange={this.handleGenreChange}
          />
        )
      });
    });
  }

 public render() {
     return (
            //...
           {this.state.asyncComponent ? (
              this.state.asyncComponent
            ) : (
              <div />
            )}
           //...
    )
 }
JillAndMe
  • 3,989
  • 4
  • 30
  • 57

1 Answers1

0
  constructor(props) {
    super(props);
    this.state = { options: null };
    //...
  }

  public componentDidMount() {
    this.props.initCreateRoomState();
    this.getSelectsIntl();
  }

  public componentWillUnmount() {}

  public componentWillReceiveProps() {
    this.getSelectsIntl();
  }

  private getSelectsIntl = () => {
    import(`../../i18n/locales/${this.props.lang}`).then(o => {
      this.setState({
        options: o.options
      });
    });
  }

  public render() {
      return (
            {this.state.options ? (
                <CustomSelects
                  options={this.state.options}
                  formattedMessageId="createroom_genre"
                  value={this.props.genre}
                  handleValueChange={this.handleGenreChange}
                />
              ) : (
                <div />
              )}
//...
JillAndMe
  • 3,989
  • 4
  • 30
  • 57