React – throw error when creating react component conditionally

I have a react component called ListChats and another called Chat.

ListChats calls the rendering of a Chat

I need that if an error occurs when rendering the Chat, ListChats knows and comes back with the list of available chats.

A possible error, its capture has not yet been implemented, would be if the user has no name, Chat captures this error and returns to ListChat.

ListChats component:

import React, { Component } from "react";
import Chat from "../Chat";
import { Button } from 'react-bootstrap';


export default class ListChats extends Component {

  constructor(props) {
    super(props);
    this.state = {
      chat: <div />
    }
    this.toSala = this.toSala.bind(this);
  }
  
  toSala() {
    //this is where I render a Chat component, I need that, if it returns an error, I set this.state.chat to "<div />" 
    this.setState({chat: <Chat/>  });
  }
  render() {
    const { chat} = this.state;
    
    return (
      <>
         <Button  onClick={this.toSala}>abrir chat</Button>
         {chat} 
      </>
    )
  }

};

Chat component

import React,{ useState, useEffect } from 'react'

const Chat = (props) => {
  const [name, setName] = useState('');
  const [room, setRoom] = useState('');
  const [users, setUsers] = useState([]);

useEffect(() => {    
 //depending on the value of a variable of mine I have the need or not to throw an error when rendering this component 
  }, []);

  return (
    <div>
      my chat
    </div>
  );
};

export default Chat;

In short, I need to throw an error in the Chat component and catch it in ListChats component

151 thoughts on “React – throw error when creating react component conditionally”

  1. I simply could not leave your web site before suggesting that I actually enjoyed the usual information an individual supply for your visitors? Is going to be back ceaselessly in order to check up on new posts.

    Reply
  2. pharmacie achard angers act therapy buddhism , therapie cognitivo-comportementale montreal therapies familiales systemiques , [url=https://kit.co/motli/strattera-livraison-canada-strattera-pharmacie-canada#]Strattera Atomoxetine 40 mg[/url] pharmacie en ligne zamst [url=https://kit.co/wophent/sildalis-vente-libre-acheter-sildalis-120-mg-pas-cher#]Acheter Sildalis 120 mg pas cher[/url] , therapies alternatives cancer pharmacie ouverte troyes , pharmacie cora. pharmacie de garde yamoussoukro aujourd’hui pharmacie auchan lac [url=https://kit.co/bertrest/chloroquine-vente-libre-chloroquine-livraison-belgique#]Chloroquine vente libre[/url] , therapie de couple forum. huraux therapies alternatives, therapie de couple lausanne pharmacie argenteuil dimanche . pharmacie en ligne reunion, pharmacie de garde marseille st antoine pharmacie victor hugo 92100 boulogne-billancourt pharmacie bailly garancia .

    Reply
  3. I simply could not go away your web site prior to suggesting that I really loved the usual info a person supply for your guests? Is gonna be back regularly to check out new posts.

    Reply
  4. You really make it appear really easy together with your presentation but I find this topic to be actually one thing that I feel I might by no means understand. It seems too complex and very vast for me. I am looking ahead for your subsequent publish, I will attempt to get the hold of it!

    Reply
  5. Hello! Do you know if they make any plugins to protect against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations?

    Reply
  6. This design is wicked! You definitely know how to keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Wonderful job. I really loved what you had to say, and more than that, how you presented it. Too cool!

    Reply
  7. Pingback: tamoxifen brand
  8. Pingback: baricitinib acr 20
  9. Pingback: mulnopiravir
  10. Pingback: aralen 100mg
  11. Pingback: clomid 12.5