kajirikajiri(wsl2)
gh-pages
gh-pages
  • Initial page
  • 🍳😋🎵
    • githubの新しい使い方
    • itunes miniplayer 前面固定(windows)
    • force darkmode chrome and disable white flash
    • wsl2 色 変更
    • quaker oats
    • 圧力鍋 ホイル焼き
  • 💪💪🏻💪🏼🦾💪🏽💪🏾💪🏿
    • gh-openの改造
    • GraphQL
  • gh-open not working with wsl2
  • react hook を5分で使う
  • 5分でLambda Storeをつかってみた
  • 実働環境で色々updateして得た知見 vue nuxt vuetify
  • dexie firebase nuxt vuetifyでオフラインpwaメモ帳作った
  • worker api batch
  • docker run syntax error
  • webpack babel
  • chainer?
  • nuxtjs firebase ui window is not defined
  • nuxt firebaseui ui broken
  • typescript 導入 実働 既存 環境
  • code-prettify nuxt
  • rustを動かしてみる
  • js 復習
  • vue The client-side rendered virtual DOM tree
  • ApexChartsがSSRで描画されない
  • atcoder
  • gitbook Page build failure
  • globalThis
  • Promise.allSettled
  • export * as ns from "module"
  • matchAll
  • js nullish
  • nullish optional chaining
  • nullish node 14 vs 13
  • nodenv global not working
GitBook提供
このページ内
  • ためしてみた
  • 参照

役に立ちましたか?

react hook を5分で使う

前へgh-open not working with wsl2次へ5分でLambda Storeをつかってみた

最終更新 5 年前

役に立ちましたか?

この記事の一部を参照して、良さそうと感じた。stateがクラス以外で使える

import React from 'react'

class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      count: 0
    }
  }

  render() {
    return (
      <>
        <p>You clicked {this.state.count} times</p>
        <button onClick={() => this.setState({ count: this.state.count + 1 })}>
          Click me
        </button>
      </>
    )
  }
}

export default App
import React, { useState } from 'react'

const App = () => {
  const [count, setCount] = useState(0)

  return (
    <>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </>
  )
}

export default App

すごい便利そう。ということで試してみた

ためしてみた

npm init next-app nextjs-blog --example "https://github.com/zeit/next-learn-starter/tree/master/learn-starter"
cd nextjs-blog
npm run dev

アクセス

これで動くものができたので、react hookを試す

現在のdirectoryはnextjs-blogだと思う

mkdir components
touch components/example.js

 おきにいりのエディタでexample.jsを編集します

import React, { useState } from 'react';

function Example() {
  // Declare a new state variable, which we'll call "count"
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

次に、pages/index.jsに2行追加

import Head from 'next/head'
import Example from '../components/example' // add

```
        <p className="description">
          Get started by editing <code>pages/index.js</code>
        </p>
        <Example/> // add

ok

画面中央にclick meボタンが追加された。

reactはconstructorを書くのが使いづらいなと思っていたが、楽になってよかった。使いやすい

参照

5分でわかるReact Hooks - QiitaQiita
http://localhost:3000localhost
http://localhost:3000/localhost
フックの導入 – React
5分でわかるReact Hooks - QiitaQiita
Learn | Next.jsvercel
Logo
Logo
Logo
Logo