Error: Invalid hook call. guys please helpme

import React, { useState ,useEffect} from 'react'
import "./Chat.css"
import { Avatar ,  IconButton } from '@material-ui/core'
import MoreVertIcon from '@material-ui/icons/MoreVert';
import SearchOutlinedIcon from '@material-ui/icons/SearchOutlined';
import AttachFileIcon from '@material-ui/icons/AttachFile';
import EmojiEmotionsOutlinedIcon from '@material-ui/icons/EmojiEmotionsOutlined';
import MicIcon from '@material-ui/icons/Mic';
import { useParams } from "react-router-dom";
import db from './firebase';
function Chat(props) {
    const [input, setInput] = useState('')
    const [seed, setSeed] = useState('');
    const { roomId } = useParams();
    const [roomName, setRoomName] =useState("")
    useEffect(() => {
      if(roomId) {
        db.collection('rooms').doc(roomId)
        .onSnapshot(snapshot =>(
          setRoomName(snapshot.data().name)
        ))  
      }
    }, [roomId] )
    
    useEffect(() => {
        setSeed(Math.floor(Math.random() * 5000));
      }, [])

    const sendMessage = (event) => {
      event.preventDefault()
      setInput("");
    }
    return <div className="chat"> 

      <div className="chat__header">
      <Avatar src={`https://avatars.dicebear.com/api/human/${seed}.svg`} />
      <div className="chat__headerInfo">
      <h3>{roomName}</h3>
      <p> last message....</p>
      </div>

      <div className="chat__headerRight">
      <IconButton> 
      <SearchOutlinedIcon />
      </IconButton>
      <IconButton>
         <AttachFileIcon/>
         </IconButton>
      <IconButton>
      <MoreVertIcon/>
      </IconButton>
      </div>
      </div>

      <div className="chat__body">
      <p className={`chat__message ${true &&
      "chat__reciever"}`}>
      <span className="chat__name">Ashu</span>
      Hey guys
      <span className ="chat__timestamp">2:52pm;
      </span>
      </p>
      </div>

      
      <div className="chat__footer">
      <EmojiEmotionsOutlinedIcon/>
      <form>
      <input value={input} 
      onChange={(event) => setInput(event.target.value)}
      placeholder="Type a message..."
      type="text" />
      <button onClick="{sendMessage}" type="submit">Send a message</button>
      </form>
      <MicIcon/>
      </div>
    </div>
        
}

export default Chat

Please help!!!!!. whenever I tries to use {useparams}
I fails and anerror is displayed:
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app
    See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

Please help me How to debug it

2 thoughts on “Error: Invalid hook call. guys please helpme”

  1. It seems like dependency react-router missing. Have you added it ? Please look into your package.json file. if react-router is missing then you need to add this package.

    Reply
  2. I don’t think you broke the rules of hooks check if You have mismatching versions of React and React DOM or you have more than one copy of react in the same app

    Reply

Leave a Comment