< createContext >
import { useState } from "react";
import { createContext } from "react";
const msContext = createContext<{ name: string; addTodo: (textId: string) => void; }>(
{
name: "ms",
addTodo: (textId) => {
return textId;
},
});
export const msContextProvider: React.FC<{ children: React.ReactNode }> = (props) => {
const [name, setName] = useState("");
const addTodo = (textId: string) => {
return textId;
};
const context: { name: string; addTodo: (textId: string) => string;} = {
name: name,
addTodo: addTodo,
};
return (
<msContext.Provider value={context}>
{props.children}
</msContext.Provider>
);
};
export default msContext
// 이후에, App.js 감싸고, 컴포넌트에서 사용하면 된다.
// 사용은 평소대로, 필요한 것이 있다면, 마우스를 위로 살포시 올려보자. 알려줄 것이다.
'React > React-TypeScript' 카테고리의 다른 글
9. Redux-toolkit 과 함께 Typescript 사용하기 (1)기본 사용 (0) | 2022.12.27 |
---|---|
8. 코드의 타입을 모르겠다고? 걱정하지 말자! 마우스를 올려봐! (0) | 2022.12.26 |
6. React와 함께 TypeScript 사용하기(5), [ State ] (1) | 2022.12.26 |
5. React와 함께 TypeScript 사용하기(4), [ props.children 의 사용, Layout 만들기] (0) | 2022.12.25 |
4. React와 함께 TypeScript 사용하기(3), [ 상향식 State 이동 (함수) 사용하기, props 와 함수 ] (0) | 2022.12.24 |
댓글