cf. js import/export cheatsheet
React의 핵심 개념
- components
- JSX
- props
- state
import React from 'react'; //필수
import { Text } from 'react-native'; //core component를 import한다
const Cat = () => {
return (
<Text>Hello, I am your cat!</Text>
);
}
export default Cat;
import React, { Component } from 'react'; //component를 import
import { Text } from 'react-native';
class Cat extends Component { //class 이름 extends Component
render() { //render 내부 코드를 리액트 요소로 렌더링한다
return (
<Text>Hello, I am your cat!</Text>
);
}
}
export default Cat;
import React from 'react';
import { Text } from 'react-native';
const getFullName = (firstName, secondName, thirdName) => {
return firstName + " " + secondName + " " + thirdName;
}
const Cat = () => {
return (
<Text>
Hello, I am {getFullName("Rum", "Tum", "Tugger")}!
</Text>
);
}
export default Cat;
import React from 'react';
import { Text, TextInput, View } from 'react-native';
const Cat = () => {
return (
<View>
<Text>Hello, I am...</Text>
<TextInput
style={{
height: 40,
borderColor: 'gray',
borderWidth: 1
}}
defaultValue="Name me!"
/>
</View>
);
}
export default Cat;
ex. Image
import React from 'react';
import { Text, View } from 'react-native';
const Cat = (**props**) => {
return (
<View>
<Text>Hello, I am **{props.name}**!</Text>
</View>
);
}
const Cafe = () => {
return (
<View>
<Cat **name="Maru"** />
<Cat name="Jellylorum" />
<Cat name="Spot" />
</View>
);
}
export default Cafe;
import React from 'react';
import { Text, View, Image } from 'react-native';
const CatApp = () => {
return (
<View>
<Image
source={{uri: "https://reactnative.dev/docs/assets/p_cat1.png"}}
style={{width: 200, height: 200}}
/>
<Text>Hello, I am your cat!</Text>
</View>
);
}
export default CatApp;
import React, { useState } from "react"; //import해서 사용한다
import { Button, Text, View } from "react-native";
const Cat = (props) => {
const [isHungry, setIsHungry] = useState(true);
//const [state변수, 갱신함수] = useState(state변수의 초기값)
//const인데 재할당 가능한 이유
: setIsHungry같은 함수가 호출되면 컴포넌트가 재렌더링된다
return (
<View>
<Text>
I am {props.name}, and I am {isHungry ? "hungry" : "full"}!
</Text>
<Button
onPress={() => {
setIsHungry(false);
}}
disabled={!isHungry} //isHungry가 false가 되면 버튼이 disabled된다
title={isHungry ? "Pour me some milk, please!" : "Thank you!"}
/>
</View>
);
}
const Cafe = () => {
return (
<>
<Cat name="Munkustrap" />
<Cat name="Spot" />
</>
);
}
export default Cafe;