reactjs 如何在React中设置if else条件

soat7uwm  于 2023-05-22  发布在  React
关注(0)|答案(1)|浏览(115)

如何使if else条件动态的基础上点击按钮,我试过,但不工作。
i tried this way condition but not work suppose like i want to click buton which is Español then i want print language spanish like other i want to click buton which is 标准粤语then i want print language cantonese like i want if else condition how can we do that .

v6ylcynt

v6ylcynt1#

check it Here

import { useCallback, useState } from "react";
import "./styles.css";

const enum enumButtonTypes {
  ESPANOL = "Español",
  CANTONESE = "标准粤语"
}
const buttons = [enumButtonTypes.ESPANOL, enumButtonTypes.CANTONESE];
export default function App() {
  const [current, setCurrent] = useState<enumButtonTypes | string>("");
  const renderButton = useCallback(() => {
    switch (current) {
      case enumButtonTypes.ESPANOL:
        return <div>{enumButtonTypes.ESPANOL}</div>;
        break;
      case enumButtonTypes.CANTONESE:
        return <div>{enumButtonTypes.CANTONESE}</div>;
        break;
      default:
        return <div>default</div>;
        break;
    }
  }, [current]);
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      {buttons.map((button) => (
        <button onClick={() => setCurrent(button)}>{button}</button>
      ))}
      <div>{renderButton()}</div>
    </div>
  );
}

相关问题