Saturday 19 October 2019

javascript - react this.setState isn't updated state

I'm trying to figure out why a call to this.setState isn't updating state.



I have the following lines method:



changeState(state, value){
this.setState({state:value}, ()=>console.log(this.state));

}


There are a few function using this, but one is a file upload component. Its a simple component that either renders a file input OR it renders an iframe, disabled input, and button. When the button is clicked I want to change the state in the parent component which then rerenders the file component and shows a file picker.



The call to this.setState seems to work ok, but when I console log state after it runs or if I stop execution before the next render takes place state is unaffected. My file upload component has a method like this:



renderField(field){
if(this.props.hasOwnProperty('file') && this.props.file){
return(



disabled
placeholder={this.props.file}
name={field.name}
type='text'
/>
this.props.changeFile(this.props.file_type, null)}>× Remove


)
}else{
return(
{...field}
type={field.type}
name={field.name}
onChange={(event) =>{
field.input.onChange(field.input.value = event.target.files[0])}
}

/>
)
}
}


when I call the method I get this output:
enter image description here



however after console logging my state is anything but changed:

enter image description here

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...