Kotlin Notebook交互式编程环境使用指南
2026/7/21 8:05:32
官方文档
资料1
资料2
函数式组件和 Class 组件是 React 中定义组件的两种主要方式,它们在语法、功能和使用场景上有一些区别。
function Welcome(props) { return<h1>Hello, {props.name}</h1>; }class Welcome extends React.Component { render() { return<h1>Hello, {this.props.name}</h1>; } }添加链接描述
importReactfrom'react';import{Text}from'react-native';constCat=()=>{return(<Text>Hello,Iam your cat!</Text>);}exportdefaultCat;在别处通过来任意引用这个组件了
importReactfrom'react';import{Text,View}from'react-native';constCat=(props)=>{return(<View><Text>Hello,Iam{props.name}!</Text></View>);}constCafe=()=>{return(<View><Cat name="Maru"/><Cat name="Jellylorum"/><Cat name="Spot"/></View>);}exportdefaultCafe;如果把 props 理解为定制组件渲染的参数, 那么state就像是组件的私人数据记录。状态用于记录那些随时间或者用户交互而变化的数据。状态使组件拥有了记忆!
importReact,{useState}from"react";import{Button,Text,View}from"react-native";constCat=(props)=>{const[isHungry,setIsHungry]=useState(true);return(<View><Text>Iam{props.name},andIam{isHungry?"hungry":"full"}!</Text><Button onPress={()=>{setIsHungry(false);}}disabled={!isHungry}title={isHungry?"Pour me some milk, please!":"Thank you!"}/></View>);}constCafe=()=>{return(<><Cat name="Munkustrap"/><Cat name="Spot"/></>);}exportdefaultCafe;