当前位置: 首页 > news >正文

网站安全建设杀毒软件网上营销培训课程

网站安全建设杀毒软件,网上营销培训课程,深圳建设工程交易服务网站,多种网站模板目录 举例说明1. 使用 class 定义2. 使用函数定义2.1 使用普通函数2.2 使用函数组件 举例说明 比如我们要定义一个计数器 Counter,它包含一个 label 和一个 button,计数器的初始值由外部传入,点击 button 计数加 1: 这虽然是个简单组件&…

目录

  • 举例说明
  • 1. 使用 class 定义
  • 2. 使用函数定义
    • 2.1 使用普通函数
    • 2.2 使用函数组件

举例说明

比如我们要定义一个计数器 Counter,它包含一个 label 和一个 button,计数器的初始值由外部传入,点击 button 计数加 1:

在这里插入图片描述

这虽然是个简单组件,但却包含了 React 定义组件的两大核心点:

  1. 属性由外部传入
  2. 状态由内部控制

组件样式:

// counter样式
const counterStyle = {backgroundColor: "orange",width: "100px",height: "100px",borderRadius: "10px",display: "flex",flexDirection: "column",alignItems: "center",justifyContent: "center",
} as React.CSSProperties;

使用组件:

<Counter initialCount={6} />

1. 使用 class 定义

// 属性
type Props = {// 初始countinitialCount: number;
};// 状态
type State = {count: number;
};// 计数器
class Counter extends Component<Props, State> {constructor(props: Props) {super(props);this.state = {count: props.initialCount,};}render() {return (<div style={counterStyle}><p>count={this.state.count}</p><buttononClick={() => {this.setState({count: this.state.count + 1,});}}>1</button></div>);}
}

2. 使用函数定义

2.1 使用普通函数

// 属性
type Props = {// 初始countinitialCount: number;
};// 计数器
function Counter(props: Props) {const [count, setCount] = useState(props.initialCount);return (<div style={counterStyle}><p>count={count}</p><buttononClick={() => {setCount(count + 1);}}>1</button></div>);
}

注:此函数返回的类型是 JSX.Element

2.2 使用函数组件

// 属性
type Props = {// 初始countinitialCount: number;
};// 计数器
const Counter = (props: Props) => {const [count, setCount] = useState(props.initialCount);return (<div style={counterStyle}><p>count={count}</p><buttononClick={() => {setCount(count + 1);}}>1</button></div>);
};

注:此函数返回的类型是 JSX.Element
若需要,可以指定函数返回的具体类型:

// 属性
type Props = {// 初始countinitialCount: number;
};// 计数器
const Counter: React.FC<Props> = (props) => {const [count, setCount] = useState(props.initialCount);return (<div style={counterStyle}><p>count={count}</p><buttononClick={() => {setCount(count + 1);}}>1</button></div>);
};

此时函数的返回值类型是 React.FC<Props>

http://www.hrbkazy.com/news/53796.html

相关文章:

  • 招聘网站怎么做吸引人如何制作一个宣传网页
  • wordpress 使用 相对路径长春关键词优化公司
  • 一个女的让我和她做优惠网站百度ai搜索引擎
  • 湖南麟辉建设集团有限公司网站网络推广有哪些渠道
  • 做网站找个人还是公司网络营销swot分析
  • 做wish如何利用数据网站网站的设计流程
  • wordpress divi南京百度seo代理
  • 欧美风格企业网站杭州seo网站推广
  • 泰安集团网站建设费用今日郑州头条最新新闻
  • 内蒙网站开发网络seo是什么
  • 互助网站开发外贸推广引流
  • 河北网站制作公司报价百度营销
  • 国内b2b网站a片天津关键词优化网站
  • 哈尔滨网站改版sem电子扫描显微镜
  • c苏宁网站开发百度搜索引擎优化方案
  • 中山模板建站软件推广的方式有哪些
  • 做源码网站赚钱吗网络营销服务的内容
  • 免费做网站tk济南专业做网站
  • 怎么做符合seo的网站中文域名的网站
  • 仙桃网站制作网站设计买外链
  • 辽宁建设工程信息网老网站深圳网络营销推广方案
  • 怎么做售房网站微信营销的成功案例
  • 企业形象宣传片搜索引擎优化的概念
  • 猪八戒做网站要多少钱百度app下载安装
  • 中国建设银行英文网站怎么seo关键词优化排名
  • 做音乐网站多少钱国外网站排名前十
  • 南昌营销型网站建设厦门百度代理公司
  • 品牌网站建设e小蝌蚪成都专门做网站的公司
  • wordpress 调用评论数量青岛seo百科
  • 论坛网站建设用工具软件做游戏推广怎么找客户