본문 바로가기
반응형

Web Front 개발공부/React.js13

리액트 타입스크립트 props 리액트 타입스크립트 props 리액트 + 타입스크립트 프로젝트에서 props를 사용하는 방법 타입과 인터페이스를 매개변수로 사용할 수 있는데 관련 기록 1. props를 3가지 타입을 받는 PropsTest라는 컴포넌트를 작성.import React from 'react';type paramType = { year: number, dept:string, imType:string}//props가 타입(인터페이스도 존재)일 경우..const PropsTest = ({year,dept,imType}:paramType) => { return ( 년도 : {year} 부서: {dept} 종류: {imType} .. 2024. 8. 28.
컴포넌트 props 간단예제 import React from 'react';// 이름 없는 함수로 컴포넌트를 정의하고, Text라는 변수에 대입한다// Text 컴포넌트는 부모로부터 'content' 라는 데이터를 받는다.const Text = (props: {content: string})=>{ // props로 부터 cntent라는 값을 꺼낸다. const {content } = props; // props로 전달된 데이터를 표시한다. return {content}}const Message = (props:{}) => { const content1 = 'This is parent component'; const content2 = 'Message uses Text Component'; ret.. 2024. 7. 14.
타입스크립트 배열과 제네릭 타입스크립트 배열과 제네릭// T는 클래스 안에서 사용하는 임시 타입 이름이다.class Queue{ // 내부에서 T 타입의 배열을 초기화 한다. private array: T[] = []; // T 타입의 값을 배열에 추가한다. push(item: T){ this.array.push(item); } // T 타입의 배열의 첫 번째 값을 꺼낸다. pop(): T | undefined{ return this.array.shift(); }}const quene = new Queue(); // 숫자 타입을 다루는 큐를 생성한다.quene.push(123);quene.push(456);quene.push(789);console.log('제네릭.. 2024. 7. 14.
타입스크립트 기본 - Class와 Enum 타입 회사에서 다음 프로젝트로 리액트를 사용하기로 했는데... 오잉 타입스크립트를 함께 사용한다. 타입스크립트가 무엇인지는 5~6년 전에 얕게 흩어보아 잘알고 있다. 리액트에서 사용할 때 큰 문제가 없도록 간단하게 공부하고 메모를 남긴다.1. Class티스트토리 코딩 첨부하기에 TypeScript가 생기다니 이런 좋은일이~~~자바와 별반 다르지 않다. 다만 this 를 반드시 써야 에러가 안난다.interface IUser{ name:string; age:number; sayHello:()=> string; //인수 없이 문자열 반환}class User implements IUser{ name: string; age: number; constructor(){ thi.. 2024. 7. 14.
리액트 타입스크립트 다국어 설정 i18next https://react.i18next.com/getting-started Getting started | react-i18next documentationThe module is optimized to load by webpack, rollup, ... The correct entry points are already configured in the package.json. There should be no extra setup needed to get the best build option.react.i18next.com 1. 리액트 프로젝트 폴더 내에서 npm install i18next react-i18next 2. src 안에 locales 폴더를 만들고 그 아래 각각 en, ko 폴더를 만든다.. 2024. 6. 25.
React Context 사용해보기 컴포넌트의 구조가 깊게 얽혀 있다면... props 유지보수가 어렵게 된다.이때, Context를 사용한다.회사에서 여러 프로젝트에 쓰인 리액트 소스를 보니 userSession을 Context 반복작업을 해놓은 것을 봤다. 예제이므로 간단하게 콤포넌트 구조를 보자면, 아래와 같다.App => Page => Header, Content, Footer 예제는 3단 구조이나 실무에서는 이렇게 간단한 시스템은 거의 없다. 1. 우선 Context를 js 파일하나 만들어 선언한다.import { createContext } from 'react';export const UserContext = createContext(null); 2. App.js 에서 작성한 UserContext를 불러온다.유저정보를 db에서.. 2024. 6. 1.
반응형