2

I have a simple react form but when I console log my object in state it comes up as empty values. I have been sitting for a while now trying to get my values in. When i console log the event I get the values in my console, but I cant console log my state object. Here is my code:

handleSubmit = (event: any) => {
event.preventDefault();
event.persist();
this.setState({
  IPObject: {
    Name: event.target[0].value,
    IPList: event.target[1].value,
    priority: event.target[2].value,
    rule: event.target[3].value
  }
})
console.log(this.state.IPObject)


render() {
return (
  <Form className="mui-form" onSubmit={this.handleSubmit}>
    <legend>Add your IP address</legend>
    <label>Your Name</label>

    <Input placeholder="Name" required={true}></Input>
    <label>Your IP Address</label>
    <Input placeholder="IP address" required={true}></Input>
    <label>Priority Rule (Add any number from 400 and above)</label>
    <Input placeholder="Priority Rule" required={true}></Input>
    <label>Subnet (Leave on 24 by default) Unless you know what you doing</label>
    <Select defaultValue="24">
      <Option value="32" label="32"></Option>
      <Option value="24" label="24"></Option>
    </Select>
    <br></br>
    <Button variant="raised">Add IP</Button>
  </Form>
);

and this is in my state:

this.state = {
  IPObject: {
    priority: "",
    IPList: "",
    rule: "",
    Name: ""
  }
}
2
  • Does this answer your question? setState doesn't update the state immediately Commented Mar 28, 2021 at 8:33
  • setState is asynchronous. You won't see the value until the next render. Commented Mar 28, 2021 at 8:34

3 Answers 3

1

setState is async function, use like this for getting result:

this.setState({...}, ()=>{console.log(...)});

and in your code:

this.setState({
  IPObject: {
    Name: event.target[0].value,
    IPList: event.target[1].value,
    priority: event.target[2].value,
    rule: event.target[3].value
  }
}, ()=>{
  console.log(this.state.IPObject)
})
Sign up to request clarification or add additional context in comments.

Comments

1

Your assumption is wrong, you can't procedurally & synchronously update the state and then log it the next line, because technically the console.log will trigger before your actual state update. If you want to track state changes, use useEffect/componentDidUpdate and track the state change.

Comments

0

Try this

handleSubmit = (event: any) => {
  event.preventDefault();
  event.persist();
  this.setState({
      IPObject: {
      Name: event.target[0].value,
      IPList: event.target[1].value,
      priority: event.target[2].value,
      rule: event.target[3].value
  }
}, () => {

 console.log(this.state.IPObject)

})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.