Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- array
- Kotlin
- fragment
- Java
- 스프링부트
- 랜덤번호
- React
- 랜덤넘버
- TextView
- 자바스크립트
- Javscript
- JavaScript
- 안드로이드
- TypeScript
- 코틀린
- RecyclerView
- nodejs
- Linux
- 리액트
- 구글맵스
- Android
- 오버라이딩
- button
- npm
- GoogleMaps
- Hook
- JS
- SpringBoot
- scrollview
- stylesheet
Archives
- Today
- Total
타닥타닥 개발자의 일상
react / TextProvider, useContext 통해서 다른 파일에 있는 값 화면에 구현하기 본문
생성문
npx react-native init sample24 --template react-native-template-typescript
생성파일
src 폴더 생성 > 그 안에 component 폴더 생성 >
component폴더안에 CountProvider, First, Second, Third, TextProvider 타입 스크립트 파일 생성
TextProvider.tsx
import React, { createContext } from "react";
import { Text } from "react-native";
export const TextContext = createContext({})
interface Props{
children: JSX.Element
}
export default function TextProvider({ children }:Props){
const text = "이것은 전달한 값입니다."
//매개변수 children에 text값을 전달
return(
<TextContext.Provider value={text}>
{children}
</TextContext.Provider>
)
}
CountProvider.tsx
import React, { createContext, useState } from "react";
import { Text } from "react-native";
export const CountContext = createContext({})
interface Props{
children: JSX.Element
}
export const CountProvider = ({ children }:Props)=>{
const [count, setcount] = useState(0)
return(
<CountContext.Provider value={[count, setcount]}>
{children}
</CountContext.Provider>
)
}
Third.tsx
import React, { useContext } from "react";
import { Text, View, Button } from "react-native";
import { CountContext } from "./CountProvider";
import { TextContext } from "./TextProvider";
export default function Third(){
const [count,setCount]:any = useContext(CountContext)
const btnClick = () =>{
setCount( (pcount:any) => pcount+1 )
}
return(
<View>
<Text>Third Component: 현재 카운트 : {count}</Text>
<Button title="+1증가" onPress={btnClick}/>
</View>
)
}
Second.tsx
import React, { useContext } from "react";
import { Text, View } from "react-native";
import Third from "./Third";
export default function Second(){
return (
<View>
<Text>Second Component</Text>
<Third/>
</View>
)
}
First.tsx
import React, { useContext } from "react";
import { Text, TextComponent, View } from "react-native";
import Second from "./Second";
import TextProvider, { TextContext } from "./TextProvider";
export default function First(){
return (
<View>
<Text>First Component</Text>
<Second/>
</View>
)
}
App.tsx
import React, { createContext, useContext } from "react";
import { Text, View } from "react-native";
import { CountProvider } from "./src/component/CountProvider";
import First from "./src/component/First";
import TextProvider from "./src/component/TextProvider";
/*
useContext, creatContext
:값 넘기기
*/
export default function App(){
return(
<View>
<CountProvider>
<First/>
</CountProvider>
</View>
)
}
Second.tsx가 Third.tsx를 전달받고 First.tsx가 Second.tsx를 전달받음
따라서 App.tsx에서 First만 전달받아도 Third,Second의 값을 모두 받을수 있다.
실행화면
버튼 누르면 숫자가 증가한다.
아이콘 정보 사이트
Ionicons: The premium icon pack for Ionic Framework
Ionicons is an open-sourced and MIT licensed icon pack.
ionic.io
'코딩 기록 > react' 카테고리의 다른 글
react / Navigator로 화면간 이동 가능하게 만들기 (0) | 2022.03.09 |
---|---|
react 메인 화면 구성하고 Navigator로 화면 이동하게 만들기 (0) | 2022.03.09 |
react UseMemo 사용하여 특정 함수만 호출되는 예제 보기 / typescript / useCallback함수 이용하여 버튼 클릭시 숫자 증가시키기 (0) | 2022.03.08 |
react 현재시간 화면에 나타나고 초마다 갱신되게하기 / typescript 시계 만들기 (0) | 2022.03.08 |
react / useEffect 써서 버튼 누를때 특정 텍스트 화면에 보이게 하기 / typescript (0) | 2022.03.08 |
Comments