next.js 物料-顺风选择选项

qlzsbp2j  于 2022-12-03  发布在  其他
关注(0)|答案(2)|浏览(125)

我在Next.JS应用程序中使用**@material-tailwind/react**。我从下面的示例中获取了以下代码:https://www.material-tailwind.com/docs/react/select。在React中,我可以在Select上使用onChange函数,然后获取选定项的值,例如“e.target.value”。它如何与此组件一起工作?

import { Select, Option } from "@material-tailwind/react";
 
export default function Example() {
  return (
    <div className="w-72">
      <Select label="Select Version">
        <Option>Material Tailwind HTML</Option>
        <Option>Material Tailwind React</Option>
        <Option>Material Tailwind Vue</Option>
        <Option>Material Tailwind Angular</Option>
        <Option>Material Tailwind Svelte</Option>
      </Select>
    </div>
  );
}
ovfsdjhp

ovfsdjhp1#

我唯一能解决这个问题的方法如下,这与你用React解决这个问题的方法有点不同。

import { Select, Option } from '@material-tailwind/react';
import { useState } from 'react';

export default function Home() {
  const [value, setValue] = useState(null);

  const handleChange = (value) => {
    console.log('value:', value);
  };

  return (
   <div className='w-72'>
       <Select label='Select Version' value={value} onChange={handleChange}>
          <Option value='123'>Material Tailwind HTML</Option>
          <Option>Material Tailwind React</Option>
          <Option>Material Tailwind Vue</Option>
          <Option>Material Tailwind Angular</Option>
          <Option>Material Tailwind Svelte</Option>
     </Select>
  </div>
  );
}
rjee0c15

rjee0c152#

import React,{useState} from "react"
    import { Select, Option } from "@material-tailwind/react";

 export default function Example() {
const [temp,setTemp] = useState();
const handleChange=(e)=>{
setTemp(e);
}
  return (
    <div className="w-72">
      <Select onChange={handleChange} label="Select Version" >
        <Option value="A">Material Tailwind HTML</Option>
        <Option value="B">Material Tailwind React</Option>
        <Option value="C">Material Tailwind Vue</Option>
        <Option value="D">Material Tailwind Angular</Option>
        <Option value="E">Material Tailwind Svelte</Option>
      </Select>
    </div>
  );
}

相关问题