{"id":13261,"date":"2021-02-27T11:10:51","date_gmt":"2021-02-27T02:10:51","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=13261"},"modified":"2023-05-27T22:12:40","modified_gmt":"2023-05-27T13:12:40","slug":"%e3%80%90react%e3%80%91typescript%e3%81%a7%e7%94%a8%e6%84%8f%e3%81%95%e3%82%8c%e3%81%a6%e3%81%84%e3%82%8b%e5%9e%8b%e4%b8%80%e8%a6%a7","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=13261","title":{"rendered":"\u3010React\u3011TypeScript\u3067\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u578b\u4e00\u89a7"},"content":{"rendered":"<h2>\u95a2\u6570\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\uff08React.FC\uff09<\/h2>\n<div>\n<div>React\u306e\u95a2\u6570\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u578b\u306b\u306a\u308a\u307e\u3059\u3002React.FunctionalComponent\u3068\u3082\u66f8\u3051\u307e\u3059\u3002<\/div>\n<h3>\u4f8b<\/h3>\n<div>\n<pre class=\"lang:default decode:true\">const App:React.FunctionalComponent = () =&gt; {\r\n  return &lt;div&gt;Hello, React!&lt;\/div&gt;;\r\n}<\/pre>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b\u3059\u308c\u3070\u578b\u5f15\u6570\u3092\u53d7\u3051\u53d6\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">interface AppProps {\r\n  msg: string;\r\n}\r\n\r\nconst App:React.FC&lt;AppProps&gt; = (props:AppProps) =&gt; {\r\n  const { msg } = props;\r\n  return &lt;div&gt;{ msg }&lt;\/div&gt;;\r\n}<\/pre>\n<\/div>\n<\/div>\n<p>\u306a\u304a\u3001\u95a2\u6570\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u30b8\u30a7\u30cd\u30ea\u30af\u30b9\u3067\u578b\u3092\u6307\u5b9a\u3059\u308b\u3068props\u306e\u65b9\u306e\u578b\u306f\u7701\u7565\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">interface AppProps {\r\n  msg: string;\r\n}\r\n\r\nconst App:React.FC&lt;AppProps&gt; = (props) =&gt; {\r\n  const { msg } = props;\r\n  return &lt;div&gt;{ msg }&lt;\/div&gt;;\r\n}<\/pre>\n<p>\u3053\u308c\u306fdefaultProps\u3068\u3044\u3046\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u5185\u90e8\u3067\u30c7\u30d5\u30a9\u30eb\u30c8\u306eprops\u306e\u578b\u3068\u3044\u3046\u5f62\u3067\u65e2\u306b\u4fdd\u6301\u3057\u3066\u3044\u308b\u304b\u3089\u306e\u6319\u52d5\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2>useState<\/h2>\n<p>React Hooks\u306euseState\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">const [value,setValue] = React.useState&lt;number&gt;(10);<\/pre>\n<p>useState\u306e\u578b\u3092\u898b\u3066\u307f\u308b\u3068\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">function useState&lt;S&gt;(initialState: S | (() =&gt; S)): [S, Dispatch&lt;SetStateAction&lt;S&gt;&gt;];<\/pre>\n<h3>React.Dispatch<\/h3>\n<p>React Hooks\u7b49\u3067\u3088\u304f\u4f7f\u308f\u308c\u3066\u3044\u308b\u578b\u3067\u3059\u3002\u5b9a\u7fa9\u3068\u3057\u3066\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">type Dispatch&lt;A&gt; = (value: A) =&gt; void;<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>\u300cA\u3068\u3044\u3046\u578b\u3092\u6307\u5b9a\u3057\u3066\u623b\u308a\u5024\u306f\u306a\u3044\u300d\u3068\u3044\u3046\u610f\u5473\u306e\u95a2\u6570<\/strong><\/span>\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>React.SetStateAction<\/h3>\n<p>\u5b9a\u7fa9\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">type SetStateAction&lt;S&gt; = S | ((prevState: S) =&gt; S);<\/pre>\n<p>\u300c\u4f55\u304b\u3057\u3089\u306e\u30c7\u30fc\u30bf\u305d\u306e\u3082\u306e\u300d\u3082\u3057\u304f\u306f\u300c\u53d7\u3051\u4ed8\u3051\u305f\u5f15\u6570\u306e\u578b\u3092\u8fd4\u3059\u4f55\u304b\u3057\u3089\u306e\u95a2\u6570\u300d\u3067\u3042\u308b\u3068\u3044\u3046\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002<\/p>\n<p>\u306a\u306e\u3067\u3001\u3053\u306e\u3088\u3046\u306b\u3082\u304b\u3051\u307e\u3059\u3057\u3001<\/p>\n<pre class=\"lang:default decode:true\">  const increment = () =&gt; {\r\n    setValue(value + 1);\r\n  }<\/pre>\n<p>\u3053\u306e\u3088\u3046\u306b\u95a2\u6570\u3092\u6e21\u3059\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">  const increment = () =&gt; {\r\n    setValue((value) =&gt; value + 1);\r\n  }\r\n<\/pre>\n<h2>useRef<\/h2>\n<p>useRef\u306e\u578b\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">function useRef&lt;T&gt;(initialValue: T): MutableRefObject&lt;T&gt;;<\/pre>\n<p>\u5f15\u6570\u306binitialValue\u3001\u623b\u308a\u5024\u306bMutableRefObject&lt;T&gt;\u3092\u53d6\u308a\u307e\u3059\u3002<\/p>\n<h3>MutableRefObject&lt;T&gt;<\/h3>\n<p>\u3053\u306e\u3088\u3046\u306acurrent\u3068\u3044\u3046\u8981\u7d20\u3092\u6301\u3064\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">interface MutableRefObject&lt;T&gt; {\r\n  current: T;\r\n}<\/pre>\n<p>\u578b\u306f\u6c7a\u307e\u3063\u3066\u3044\u306a\u3044\u306e\u3067number\u3067\u3082string\u3067\u3082\u4f55\u3067\u3082\u6e21\u305b\u307e\u3059\u3002<\/p>\n<h2>useEffect<\/h2>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u578b\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">function useEffect(effect: EffectCallback, deps?: DependencyList): void;<\/pre>\n<h3>EffectCallback<\/h3>\n<p>\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u3067\u3059\u3002\u30ec\u30f3\u30c0\u30fc\u3059\u308b\u5ea6\u306b\u3053\u3053\u3067\u6e21\u3057\u305f\u95a2\u6570\u306e\u5185\u5bb9\u3092useEffect\u304c\u5b9f\u884c\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">type EffectCallback = () =&gt; (void | Destructor);<\/pre>\n<p>\u5f15\u6570\u306f\u306a\u304f\u623b\u308a\u5024\u306fvoid\u578b\u307e\u305f\u306fDestructor\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<h3>DependencyList<\/h3>\n<pre class=\"lang:default decode:true \">type DependencyList = ReadonlyArray&lt;any&gt;;<\/pre>\n<h4>ReadonlyArray&lt;any&gt;<\/h4>\n<p>\u304b\u306a\u308a\u5927\u304d\u306a\u578b\u5b9a\u7fa9\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u4e00\u8a00\u3067\u8a00\u3048\u3070\u3001<span style=\"color: #ff0000;\"><strong>\u578b\u5f15\u6570T\u3067\u69cb\u6210\u3055\u308c\u308breadonly\u306a\u914d\u5217<\/strong><\/span>\u306b\u306a\u308a\u307e\u3059\u3002\u8aad\u307f\u53d6\u308a\u5c02\u7528\u306e\u578b\u306b\u306a\u308b\u306e\u3067\u5024\u306e\u66f4\u65b0\u304c\u3067\u304d\u307e\u305b\u3093\u3002<\/p>\n<pre class=\"lang:default decode:true\">interface ReadonlyArray&lt;T&gt; {\r\n    \/**\r\n     * Gets the length of the array. This is a number one higher than the highest element defined in an array.\r\n     *\/\r\n    readonly length: number;\r\n    \/**\r\n     * Returns a string representation of an array.\r\n     *\/\r\n    toString(): string;\r\n\r\n   \u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u4e2d\u7565\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\r\n}<\/pre>\n<h2>input\u8981\u7d20\u306eref<\/h2>\n<p>input\u8981\u7d20\u306eref\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">    interface ClassAttributes&lt;T&gt; extends Attributes {\r\n        ref?: LegacyRef&lt;T&gt;;\r\n    }<\/pre>\n<div>\n<h3>LegacyRef<\/h3>\n<p>string\u3068Ref\u306e\u3069\u3061\u3089\u304b\u3092\u53d6\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002\u4e00\u5fdc\u3001string\u3082\u53d6\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u304c\u975e\u63a8\u5968\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<div>\n<pre class=\"lang:default decode:true \">type LegacyRef&lt;T&gt; = string | Ref&lt;T&gt;;<\/pre>\n<h4>Ref<\/h4>\n<p>RefObject\u306fuseRef\u3067\u4f5c\u3089\u308c\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u306a\u306e\u3067input\u8981\u7d20\u306eref\u306fuseRef\u306e\u623b\u308a\u5024\u3092\u53d6\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">type Ref&lt;T&gt; = RefCallback&lt;T&gt; | RefObject&lt;T&gt; | null;<\/pre>\n<h2>useReducer<\/h2>\n<p>\u30aa\u30fc\u30d0\u30fc\u30ed\u30fc\u30c9\u3055\u308c\u30665\u3064\u4f4d\u5b9a\u7fa9\u304c\u3042\u308b\u306e\u3067\u3059\u304c\u3001\u305d\u306e\u3046\u3061\u3088\u304f\u4f7f\u308f\u308c\u308b\u578b\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">    function useReducer&lt;R extends Reducer&lt;any, any&gt;&gt;(\r\n        reducer: R,\r\n        initialState: ReducerState&lt;R&gt;,\r\n        initializer?: undefined\r\n    ): [ReducerState&lt;R&gt;, Dispatch&lt;ReducerAction&lt;R&gt;&gt;];<\/pre>\n<\/div>\n<h3>R extends Reducer&lt;any,any&gt;<\/h3>\n<p>Reducer\u306e\u5b9a\u7fa9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">type Reducer&lt;S, A&gt; = (prevState: S, action: A) =&gt; S;<\/pre>\n<p>extends\u3067Reducer\u3092\u7d99\u627f\u3057\u3066\u3044\u308b\u306e\u3067<span style=\"color: #ff0000;\"><strong>Reducer\u3068\u4e92\u63db\u6027\u304c\u306a\u3044\u3068\u3044\u3051\u306a\u3044<\/strong><\/span>\u3068\u3044\u3046\u610f\u5473\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>ReducerState&lt;R&gt;<\/h3>\n<p>ReducerState\u306f\u72b6\u614b\u306e\u578b\u3067\u3042\u308b\u3053\u3068\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true  \">type ReducerState&lt;R extends Reducer&lt;any, any&gt;&gt; = R extends Reducer&lt;infer S, any&gt; ? S : never;<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"\u95a2\u6570\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\uff08React.FC\uff09 React\u306e\u95a2\u6570\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u578b\u306b\u306a\u308a\u307e\u3059\u3002React.FunctionalComponent\u3068\u3082\u66f8\u3051\u307e\u3059\u3002 \u4f8b const App:React.FunctionalCompon [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[116,101],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13261"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13261"}],"version-history":[{"count":9,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13261\/revisions"}],"predecessor-version":[{"id":20432,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13261\/revisions\/20432"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13261"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}