用钩子创建React功能组件,然后将其发布在NPM软件包中

发布于 2025-01-28 01:25:33 字数 1240 浏览 6 评论 0原文

我尝试创建自己的NPM包。 我创建并发布了我的组件,它正在工作,但是当我在组件中添加使用效果时,我会有错误。 什么是去吗?

import React, { FC, useEffect, useState  } from 'react';
import './Button.scss';

export interface ButtonProps {
  children: any;
  styles?: Array<string>;
}

const Button: FC<ButtonProps> = (
  {
    children,
    styles,
    ...props
  }) => {

  const [active, setActive] = useState(null);
  const root_styles = ['pef_button'];
  
  useEffect(()=>{
    console.log('JK:DAHJS:JDKHA:SKJhd');
    
  },[])

  if(styles){
    for (let i = 0; i < styles.length; i++){
      root_styles.push(styles[i]);
    }
  }
    
  return(
    <button {...props} className={root_styles.join(' ')} >
      {children}
    </button>
  );
};

export default Button;

我确实在应用程序中导入此组件,并且有错误

import React, {useContext, useState, useEffect} from 'react';

import {Button, Input} from 'My[![enter image description here][1]][1]-react-library'

const MainPage: React.FunctionComponent = () => {


  return (
    <div>
      <div>
        <Button   >
          zxc
        </Button>
      </div>
    </div>
  );
};
    
export default MainPage;

,也许我应该使用组件类代替功能组件

I try to create own npm pack.
I created and published my component, it is working, but when I add UseEffect in my component I have errors.
What is goin on?

import React, { FC, useEffect, useState  } from 'react';
import './Button.scss';

export interface ButtonProps {
  children: any;
  styles?: Array<string>;
}

const Button: FC<ButtonProps> = (
  {
    children,
    styles,
    ...props
  }) => {

  const [active, setActive] = useState(null);
  const root_styles = ['pef_button'];
  
  useEffect(()=>{
    console.log('JK:DAHJS:JDKHA:SKJhd');
    
  },[])

  if(styles){
    for (let i = 0; i < styles.length; i++){
      root_styles.push(styles[i]);
    }
  }
    
  return(
    <button {...props} className={root_styles.join(' ')} >
      {children}
    </button>
  );
};

export default Button;

I do import this component in my app and have error

import React, {useContext, useState, useEffect} from 'react';

import {Button, Input} from 'My[![enter image description here][1]][1]-react-library'

const MainPage: React.FunctionComponent = () => {


  return (
    <div>
      <div>
        <Button   >
          zxc
        </Button>
      </div>
    </div>
  );
};
    
export default MainPage;

Maybe I should use component classes instead of functional-components

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏见 2025-02-04 01:25:33

您用什么打包?使用汇总时,我遇到了同样的问题。我通过在lollup.config.js中添加React和React-Dom将其添加到外部来解决。

export default {
    ...,
    external: [
        'react',
        'react-dom',
    ]
}

What are you using to package it? I had the same issue while using Rollup. I solved it by adding react and react-dom to external in rollup.config.js.

export default {
    ...,
    external: [
        'react',
        'react-dom',
    ]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文