Closed26

TypeScript/Next.js/ChakraUIを使ったクソアプリを作る

kazskazs

最近筋トレにハマっているので、それを題材にしたクソアプリを作る。
React・TypeScriptの学習したいというのもあるので、Next.jsを使う。

CSS frameworkはChakra UIを選定。特別な理由はない。

kazskazs

アプリ名 KinniQ
筋トレに関するQuizを出し、正誤によって筋トレメニューを決定する。

kazskazs

画面遷移図

超シンプルだが、Reactは始めてだしフロントの技術もデザインも苦手なのでこれくらいで十分

kazskazs

プロジェクトのセットアップをしてみる
まずはNext.js + TypeScriptから

yarn next-app kinniq-frontend

早速失敗している

kazskazs

nodeのバージョンのせいだったのでnodenvで16.13.1を入れて解消

kazskazs

次にTypeScriptの導入
空のtsconfig.jsonを作成して yarn devを実行する

❯ touch tsconfig.json

❯ yarn dev
yarn run v1.22.17
$ next dev
warn  - Port 3000 is in use, trying 3001 instead.
ready - started server on 0.0.0.0:3001, url: http://localhost:3001
It looks like you're trying to use TypeScript but do not have the required package(s) installed.

Please install typescript and @types/react by running:

        yarn add --dev typescript @types/react

If you are not trying to use TypeScript, please remove the tsconfig.json file from your package root (and any TypeScript files in your pages directory).

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

言われた通りyarn add --dev typescript @type/reactを実行

kazskazs
kazskazs
yarn add '@chakra-ui/react' '@emotion/react@^11' '@emotion/styled@^11' 'framer-motion@^5'

で必要なパッケージを導入。

その後、ドキュメント通りにChakraProviderを_app.tsxに追記

kazskazs

とりあえずトップページのフォントとかが変わったが、もう少し何か書いて試してみる。

kazskazs

簡単なトップページをChakraUIコンポーネントで作ってみた

    <Box>
      <Box bg='orange' w='100%' p={4} color='white'>
        <Text>This is KinniQ!!</Text>
      </Box>
      <Container>
        <Text pt={5} fontSize='5xl' align='center'>
          KinniQ
        </Text>
        <Box pt={10} align='center'>
          このアプリケーションは筋肉に関する知識を問うクイズアプリです。
        </Box>

        <Center pt={7}>
          <Button colorScheme="teal" color="white">
            クイズに挑戦する
          </Button>
        </Center>
      </Container>
    </Box
kazskazs

ざっくりとドキュメントのどこを見て試行錯誤すればよいかはわかったので次に進む

kazskazs

できればeslintとprettierの導入もちゃんとやろう

kazskazs

eslintはnextに標準搭載されているが、configまでやりだすと時間かかるので一旦スルー

kazskazs

まずはメインとなるクイズの画面とロジックを作っていくか

kazskazs

超雑だが、クイズ画面を作成した
APIからはクイズ文と答えを受け取り、正解か不正解かによって文面等を変更する

kazskazs

クイズデータの取得するところ

kazskazs

この程度のためにわざわざRailsでAPI作るのもなーと思ってしまうな
またFirebaseでやるか?

このスクラップは2024/02/18にクローズされました