Set default usestate with array of objects from props

You need to add another useEffect hook:

useEffect(() => {
  if (props.fileNamesStatus && props.fileNamesStatus.length) {
    setResult(props.fileNamesStatus.map((file, i) => {
      return {
        key: file.fileStatus
      }
    }))
  }
}, [props.fileNamesStatus])

But even with this change there is a chance that in your render props and state will be out of sync, so you should add additional check console.log(result[i] ? result[i].key : 'out of sync')

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top