React hooks useState Array

前端 未结 5 1154
眼角桃花
眼角桃花 2021-01-30 07:39

I tried looking for resetting useState array values in here but could not find any references to array values.

Trying to change the drop down value from ini

5条回答
  •  心在旅途
    2021-01-30 08:05

    The accepted answer shows the correct way to setState but it does not lead to a well functioning select box.

    import React, { useState } from "react"; 
    import ReactDOM from "react-dom";
    
    const initialValue = { id: 0,value: " --- Select a State ---" };
    
    const options = [
        { id: 1, value: "Alabama" },
        { id: 2, value: "Georgia" },
        { id: 3, value: "Tennessee" }
    ];
    
    const StateSelector = () => {   
       const [ selected, setSelected ] = useState(initialValue);  
    
         return (
           
    ); }; const rootElement = document.getElementById("root"); ReactDOM.render(, rootElement);

提交回复
热议问题