Search code examples
javascriptreactjsflux

setState is not working inside event function


I have declared on constructor,

this.onProjectSelect.bind(this);

Ths is outside render and outside of constructor,

onProjectSelect(event) {
    this.setState({project: event.target.value});
    event.preventDefault();
}

JSX View

<Select2
data={BasicStore.projectsSelect2Format}
onChange={this.onProjectSelect}
options={{
    placeholder: 'Search Project',
}}
/>

I don't understand my mistakes here. Please help me to to get out of here. Thanks


Solution

  • Are you just calling this.onProjectSelect.bind(this); or are you assigning it back go this.onProjectSelect? The bind function returns a new function, so I would expect to see:

    this.onProjectSelect = this.onProjectSelect.bind(this);