0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

React入門(useState, useEffect)

Posted at

useStateの使い方

変更前、変更後の状態の管理する機能です。
今回の場合はcheckboxの状態を管理しています。

親クラスApp.js
子クラスTodos.js

Todos.jsx

import React, { useState } from 'react';


const Todos = () => {
  const [Task, setTask] = useState(false); //初期値(false)
       //変更前  変更後
  return (
    <div>
      <input 
         type="checkbox" 
         checked={Task} 
         id="check" 
         onClick={() => setTask(!Task)}/>
    </div>
  )
};

export default Todos;
App.js
import React from 'react';
import Todos from './Todos';


return (
    <>
     <Todos />
    </>
  )
};

export default App;

useEffectのライフサイクル

Mountingは初期値
Unmountingは初期値から変更された値

useEffect

import { useEffect } from 'react';

useEffect(() => {
  // MountingとUnmountingに呼ばれる
})

useEffect(() => {
  // Mountingの時に呼ばれる
}, [])

useEffect(() => {
  // Mountingと引数の時に呼ばれる
}, [引数])

useEffect(() => {
    // Mountingの時に呼ばれる
  return () => {
    //Unmountingが呼び出される
   }
})
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?