“Perbarui Array Usestate” Kode Jawaban

Perbarui Array Usestate

<body>
<div id="root"></div>

<script>
const { useState } = React;

const App = () => {
    const [myArray, updateMyArray] = useState([]);

    const onClick = () => {
        updateMyArray( arr => [...arr, `${arr.length}`]);
    };
    return [
        <input type="button" onClick={ onClick } value="Update" />,

        <div>{myArray.map( e =>
          <div>{ e }</div>
        )}
        </div>
    ];
}

ReactDOM.render(
    <App />,
    document.getElementById("root")
);
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.1/umd/react-dom.production.min.js"></script>
</body>
King Bean

Perbarui array objek dengan status penggunaan

const toggleDone = (id) => {
  console.log(id);

  // loop over the todos list and find the provided id.
  let updatedList = state.todos.map(item => 
    {
      if (item.id == id){
        return {...item, done: !item.done}; //gets everything that was already in item, and updates "done"
      }
      return item; // else return unmodified item 
    });

  setState({todos: updatedList}); // set state to new object with updated list
}
Condemned Cow

Jawaban yang mirip dengan “Perbarui Array Usestate”

Pertanyaan yang mirip dengan “Perbarui Array Usestate”

Lebih banyak jawaban terkait untuk “Perbarui Array Usestate” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya