React JS : Array Spread using prevState

Get object property values to a new array from an array of objects. Use state in react js and update array by looping through object properties. Set state by spreading the prevState (previous state values) of an Array.

Example 1:

                    props.setResidentList(prevState => [...prevState, student])

Example 2:

const [tagList, setTagList] = useState<string[]>([])
    
useEffect(() => {

        itemList?.forEach(item => setTagList(prevState => [...prevState, item?.category?.name] ) )

    }, [itemList])

About the Author: smartcoder

You might like

Leave a Reply

Your email address will not be published. Required fields are marked *