简体   繁体   English

在 React 组件中使用 Material UI 选项卡 handleChange function 中的 onChange

[英]Use onChange in Material UI tabs handleChange function in React component

Currently I have a certain navigation setup like this:目前我有这样的导航设置:

      <div>
        {letters?.map((letter) => {
          return (
            <button
              type="button"
              key={item.value}
              data-active={value === item.value}
              onClick={() => {
                if (onChange) {
                  onChange(item.value);
                }
              }}
            >
              {item.label}
            </button>
          );
        })}
      </div>

I want to change it, to use Material UI tabs .我想改变它,使用Material UI tabs

I have the following:我有以下内容:

const MyComponent = ({ firstLetters }: Props) => {
  const [value, setValue] = useState(0);

  const handleChange = (_event: React.ChangeEvent<{}>, newValue: string) => {
    console.warn(newValue);
    setValue(newValue);
  };

  return (
        <Tabs css={styles.tabsRoot} value={value} onChange={handleChange}>
          {firstLetters?.map((item) => {
            return (
              <Tab
                key={item.value}
                label={item.label}
                disableRipple
              />
            );
          })}
        </Tabs>
  )
};

How do I use onChange like my previous onClick function callback into the handleChange function?如何像我之前的onClick function 回调到handleChange function 一样使用onChange

According to the MUI docs , you should have value and onChange props in the Tabs component.根据MUI docs ,您应该在Tabs组件中有valueonChange道具。 So, you should remove the onClick prop from the Tab components, and add the aforementioned Tabs props like so:因此,您应该从Tab组件中删除onClick道具,并添加上述Tabs道具,如下所示:

<Tabs value={value} onChange={handleChange}>
  {letters?.map((item) => {
    return (
      <Tab
        key={item.value}
        label={item.label}
        disableRipple
        value={item.value}
      />
    );
  })}
</Tabs>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 材料 ui slider 组件 onChange 事件不适用于 Fromik handleChange 事件 - material ui slider component onChange event not working with Fromik handleChange event 可以在反应组件之外使用handleChange function 吗? - Possible to use handleChange function outside of react component? react-hook-form(V7):如何通过 onChange 将 function 传递给嵌套的 Material UI 组件 - react-hook-form(V7): How to pass a function via onChange to a nested Material UI component 材质 ui Select 手柄更改 - material ui Select handleChange 材质 UI 菜单组件中的 onChange - onChange in Material UI Menu component 具有自定义材质 UI 组件的 Formik 表单未调用 onChange function - Formik form with custom material UI component not calling onChange function MaterialUI的SelectField组件中的onChange / React不接受父组件的onChange - onChange In SelectField Component For Material-UI / React is not accepting onChange from parent Component React onChange 事件返回 TypeError: props.handleChange is not a function - React onChange event returns a TypeError: props.handleChange is not a function 如何在 React 中制作可重用的 Material UI 选项卡组件 - How to make a reusable Material UI tabs component in React 在 Material UI React Tabs 组件中的标签旁边添加动态值 - Add dynamic value beside label in Material UI React Tabs Component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM