Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
react.js state function only reacts to statechange when called from button but not from function

I have this state and pass it down to ChatIO

var [chatRoomIDs, setIDofRoom ] = React.useState(0); 

const changeChatRoomID = (id) => {
    setIDofRoom(chatRoomIDs = id); // write new state to state name. this function can be handed down to any component 
}

    return (
        <div className="container">
            <ChatIO chatroomID={chatRoomIDs} chatRoomFunction={changeChatRoomID}/>
        </div>
    )

When I am in ChatIO and do this:

        <Button onClick={() => props.chatRoomFunction(100283100)}variant="contained">senden</Button>

Everything works fine. The state is changed to ID 100283100 and react reacts to the state change.

But when I do this:

                <Button onClick={() => buttonClick()}variant="contained">senden</Button>

function buttonClick() {
    SendMessage()
    props.chatRoomFunction(100283100)

}

The function from the parent is called (i can see with the help of alerts) but react does NOT react to the state change.

How can I alter my code that react reacts (I hate saying that over and over)... to the state change when called from within a function NOT from a button click?

*** LITTLE EDIT ***

I found out that it sometimes rerenders the component and sometimes does not. So I must be doing something odd here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Whatever.js

var [chatRoomIDs, setIDofRoom ] = React.useState(0); 

return (
  <div className="container">
    <ChatIO chatroomID={chatRoomIDs} chatRoomFunction={setIDofRoom}/>
  </div>
)

ChatIO.js

const ChatIO = (props) => {
  function changeChatRoomId(id) {
    // sendMessage()
    // props.chatRoomFunction(100283100)  
    
    // but you probably want that 
    props.chatRoomFunction(id)
  }

  return (
    <Button onClick={() => changeChatRoomId(100283100`enter code here`)}variant="contained">senden</Button>
  )
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!