reactjs - 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,
};
如何向我的组件添加状态?
解决方案
导入 useState 钩子
import React,{useState} from 'react';
然后在您的组件中初始化状态..
const [value,setValue]=useState(' ')
推荐阅读
- python - 我想在 Python 中为 CNN 自动创建权重
- ruby-on-rails - 使用 Devise 时发生 UrlGenerationError
- typescript - 打字稿 - 默认参数类型“{}”不可分配给
- javascript - 使用 Excel VBA webcrawler 将 Javascript BLOB 从画布下载到 PDF
- html - ng-repeat:动态地为表格背景着色
- javascript - ReasonML 对命令式 vanilla JavaScript 的性能
- html - css 更改类的大小不起作用
- php - 带会话的 PHP 登录验证
- typescript - es6 导入。有没有办法在“泛型”中使用“命名空间”作为“类”?
- docker - Symfony4 / Docker:SQLSTATE [HY000] [2002] 连接被拒绝