I have the following react component (it works), but I am using a cb for setState.
I would like to know how to refactory the code removing the cb from the code:
this.setState({ viewer: null, document: null }, () => this.getViewer(type, item))
export class DocumentComponent extends React.Component {
constructor(props) {
super(props)
this.state = {
document: null,
viewer: null,
}
}
getViewer(type, item) {
let node = null
switch (type) {
case 'image':
node = (
<Imager url={item.src} />
)
this.setState({ viewer: node, document: item })
break
default:
this.setState({ viewer: null, document: null })
return null
}
}
openViewer(type, item) {
this.setState({ viewer: null, document: null }, () => this.getViewer(type, item))
}
handlerOnClick(item: Object) {
this.openViewer('image', item)
}
render() {
const { tileData, classes } = this.props
return (
<div className={classes.root}>
<Thumbnailss tileData={tileData} handlerOnClick={item => this.handlerOnClick(item)} />
{this.state.viewer}
</div>
)
}
}
export default DocumentComponent
this.setState({ viewer: null, document: null }, ...)? Why not just executethis.getViewer(type, item)directly instead?this.getVieweris already doing that as well. It either creates a new component or sets the state tonull. I don't see why a previous call to set everything tonullis necessary.