0

I've never encountered it before, I'm trying to use a simple setState function but from some unknown reason the state won't update:

my state:

this.state = {
    errors: {}
}

my function:

validateFields: function() {
    var data = deepCopy(this.state.sites)
    var newErrors = {}

    Object.values(data).map((list) => {
        list.map((argument) => {
            if (argument['value'] == '')
                newErrors[argument['key']] = 'empty field.'
        })
    })

    if (Object.keys(newErrors).length != 0) {
        this.setState({errors: newErrors})
        return false
    }

    return true
},

--- edit --- I'm calling the function like this:

if (this.validateFields())
    this.handleSave()

And when I'm trying to console.log like:

this.setState({errors: newErrors}, () => console.log(this.state.errors))

I'm still ending up with empty results on the state, but the newErros is full when I'm trying to console.log it.

if I'm trying to console.log(this.state.errors) after the function ended I'm getting {}

Any ideas?

Elon Salfati
  • 1,537
  • 6
  • 23
  • 46

0 Answers0