小编ANT*_*SEO的帖子

如何输入 React.useState() ,它可以使用 Typescript 获取多种类型作为其值

我正在尝试输入 useState() 挂钩,它可以获取数字和字符串类型作为其值。

export interface Select {
  selectValue: React.Dispatch<React.SetStateAction<number | string>>;
  ...
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试像这样使用上面的组件,

const [value, setValue] = React.useState(0);
<Select selectValue = {setValue} />
Run Code Online (Sandbox Code Playgroud)

但它给了我这个错误。

Type 'Dispatch<SetStateAction<number>>' is not assignable to type 'Dispatch<SetStateAction<string | number>>'.
  Type 'SetStateAction<string | number>' is not assignable to type 'SetStateAction<number>'.
    Type 'string' is not assignable to type 'SetStateAction<number>'.
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个错误?

typescript reactjs

6
推荐指数
1
解决办法
6554
查看次数

如何使用 Storybook.js 通过 React Portal 预览组件

我使用 popper.js 实现了 Popper 组件,并使用 React 的 Portal 来渲染 popper 元素。

现在我想预览我的 popper 组件,但它无法在 Storybook.js 上创建门户

这是我的方法。

.storybook/preview.js

import { ThemeProvider } from 'emotion-theming';
import { Global } from '@emotion/react';
import { theme, normalize } from 'custom-theme';

export const parameters = {
  actions: { argTypesRegex: '^on[A-Z].*' },
  controls: {
    matchers: {
      color: /(background|color)$/i,
      date: /Date$/,
    },
  },
};

export const decorators = [
  Story => (
    <>
      <ThemeProvider theme={theme}>
        <Global styles={normalize} />
        {Story()}
        <div id="popper-root"></div>
      </ThemeProvider>
    </>
  ),
];
Run Code Online (Sandbox Code Playgroud)

门户网站.tsx …

javascript reactjs storybook react-portal

5
推荐指数
1
解决办法
4162
查看次数