Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
Useeffect not getting called immedietly as dependency value changes

I am very new to react. I am experimenting with react and trying to undertand it's concepts right now.

I have a component that has two states,Name and room. I am using setstate to set room and name values, I have a 2 useeffects 1st one runs at initial render and gets the name and room value. second one is supposed to render as soon name or room changes

const Chat = ({location}) => {
    const [userName, setUserName] = useState("");
    const [userRoom, setUserRoom] = useState("");
    useEffect(() => {
        // console.log("initial use effect called")
        const {name,room} = queryString.parse(location.search)
        setUserName(name)
        setUserRoom(room)
    }, [])
    useEffect(()=>{
        console.log('name or room changed')
    },[userName,userRoom])
    return (

        <div>
           component
        </div>
    )
}

Since I am setting name and room one after another I expected second use effect to be called three times

(first time on initial render,second time when name changes,third time when room changes)

but it is only called twice. Also, if I add a timeout in initial useeffect,

second useeffect get called three times

const Chat = ({location}) => {
    const [userName, setUserName] = useState("");
    const [userRoom, setUserRoom] = useState("");
    useEffect(() => {
        // console.log("initial use effect called")
        const {name,room} = queryString.parse(location.search)
        setUserName(name)
        setTimeout(()=>{
        setUserRoom(room)
          },0)
        
    }, [])
    useEffect(()=>{
        console.log('name or room changed')
    },[userName,userRoom])
    return (

        <div>
           component
        </div>
    )
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

React does state updates in batches. Which means, when you are setting state multiple times, it will update both the state in a single operation, resulting in reduced re-rending (which is good in most of the cases). You can learn more about this here.

about 4 years ago · Juan Pablo Isaza Relatório

0

When setState is called multiple times in React event handler or synchronous lifecycle method, it will be batched into a single update. That's why you are getting two logs, initial render causes first log and setState causes second log and when you called useState inside setTimeOut then it logged as you expected because setTimeOut is an asynchronous operation and it behaves like this with other asynchronous operations such as async/await, then/catch, fetch, etc. because Separate state updates will not be batched in asynchronous operations.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda