📜  react 中的动态 useState - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:28.556000             🧑  作者: Mango

代码示例1
const data = ["James", "John", "Jessica", "Jamie"];

function App() {
  const [visibilities, setVisibilities] = React.useState(() => data.map((x) => true));

  const handleClick = (event) => {
    const index = parseInt(event.currentTarget.dataset.index, 10);
    const newVisibilities = [...visibilities];
    newVisibilities[index] = !newVisibilities[index];
    setVisibilities(newVisibilities);
  };

  return (
    
{data.map((value, index) => (

Hello {value}, you are {visibilities[index] ? "visible" : "hidden"}!

))}
); } ReactDOM.render(, document.querySelector("main"));