赞
踩
我们让入口指向我们自己写的组件
import {AppRegistry} from 'react-native';
import App from './App';
import myApp from './MyDemo/myDemo01';
import {name as appName} from './app.json';
AppRegistry.registerComponent(appName, () => myApp);
import React from 'react';
import { Text } from 'react-native';
const Cat = () => {
return (
<Text>我是猫我是猫我是猫我是猫我是猫我是猫</Text>
);
}
export default Cat;
(1)要定义一个组件,首先需要导入React
import React from 'react';
(2)要使用文本,就导入文本组件
import { Text } from 'react-native';
(3)一个简单的函数,可以作为一个组件
const Cat = () => {};
(4)函数的返回值,被渲染成为一个React元素
const Cat = () => {
return (
<Text>我是猫我是猫我是猫我是猫我是猫我是猫</Text>
);
}
(5)这样,我们就定义好了一个元素,但是为了给其他地方用,我们需要导出,导出方法如下:
export default Cat;
(6) <Text>我是猫我是猫我是猫我是猫我是猫我是猫</Text>
这是一种简化React元素的写法,这种语法名字叫做JSX
(1)JSX本质上就是JavaScript,所以可以使用变量。
const Cat = () => {
const name = "kitty";
return (
<Text>我是猫我是猫我是猫我是猫我是猫我是猫,我名字叫{name}</Text>
);
}
(2)也可以调用一个函数
const Cat = () => {
const name = "kitty";
return (
<Text>我是猫,我名字叫{getCatName("张三","家的猫")}</Text>
);
}
const getCatName = (firstName, secondName) => {
return firstName + secondName;
}
(3)自定义组件
import React from 'react'; import { Text,TextInput,View } from 'react-native'; const Cat = () => { return ( <View> <Text>我是猫,我名字叫{getCatName("张三","家的猫")}</Text> <TextInput>你好</TextInput> </View> ); } const getCatName = (firstName, secondName) => { return firstName + secondName; } export default Cat;
const Cat = (props) => { return ( <View> <Text>我是猫,我名字叫{props.name}</Text> <Text>我是猫,我年龄{props.age}岁</Text> </View> ); } const Animal = () => { return( <View> <Cat name = "哈哈哈" age = '10' height = '3.4'></Cat> <Cat name = "嘿嘿嘿" age = '20'></Cat> </View> ) } export default Animal;
const CapImg = () => { return( <View> <Image source = { { uri: "https://reactnative.dev/docs/assets/p_cat1.png" } } style = { { width: 200, height: 200 } } /> </View> ) } const Cat = (props) => { return ( <View> <Text>我是猫,我名字叫{props.name}</Text> <Text>我是猫,我年龄{props.age}岁</Text> </View> ); } const Animal = () => { return( <View> <CapImg></CapImg> <Cat name = "哈哈哈" age = '10' height = '3.4'></Cat> <Cat name = "嘿嘿嘿" age = '20'></Cat> </View> ) } export default Animal;
效果图:
{{}}
,Image里面也是{{}}
{
width: 200,
height: 200
}
{
uri: "https://reactnative.dev/docs/assets/p_cat1.png
}
<Image
source = {
{
uri: "https://reactnative.dev/docs/assets/p_cat1.png"
}
}
style = {
{
width: 200,
height: 200
}
}
/>
import React,{useState} from 'react'; import { Text,TextInput,View,Image,Button } from 'react-native'; const CapImg = () => { return( <View> <Image source = { { uri: "https://reactnative.dev/docs/assets/p_cat1.png" } } style = { { width: 200, height: 200 } } /> </View> ) } const Cat = (props) => { const [isHungry, setIsHungry] = useState(true); return ( <View> <Text>我是猫,我名字叫{props.name}</Text> <Text>我是猫,我年龄{props.age}岁</Text> <Button onPress={() => { setIsHungry(!isHungry) }} title = {isHungry ? "I'm Hungry" : "饱了,去玩耍"} ></Button> </View> ); } const Animal = () => { return( <View> <CapImg></CapImg> <Cat name = "哈哈哈" age = '10' height = '3.4'></Cat> <Cat name = "嘿嘿嘿" age = '20'></Cat> </View> ) } export default Animal;
import React, { useState } from 'react';
const [isHungry, setIsHungry] = useState(true);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。