首页 > 解决方案 > React组件如何添加状态

问题描述

我正在使用以下内容创建标签组件:

/components/Tag/index.js

import React from 'react';
import PropTypes from 'prop-types';
import './Tag.scss';

export const Tag = ({ color, ghost, size, label, ...props }) => {
  return (
    <div
      className="tag-primary"
      {...props}
    >
      {label}
    </div>
  );
};

Tag.propTypes = {
  label: PropTypes.string.isRequired,
  onClick: PropTypes.func
};

Tag.defaultProps = {
  color: 'teal',
  size: 'medium',
  onClick: undefined,
};

如何向我的组件添加状态?

标签: reactjscomponents

解决方案


导入 useState 钩子


import React,{useState} from 'react';

然后在您的组件中初始化状态..

const [value,setValue]=useState(' ')

推荐阅读