Closed17

Next.js x Firebase Book Log

まった | maztak.ethまった | maztak.eth

1回user.tsのようにファイルを作っちゃうと、improtするときに../models/Userではエラーになることがあるが再起動とかしてると治る

import { User } from '../models/User'
まった | maztak.ethまった | maztak.eth

ここわからん
useEffectとrecoilを使ってるauthentication.tsをimportしてるわけでもないのに、なんでホットリロードしたらState?が変わるんだ??

修正してファイルを保存すると、ホットリロードにより画面が更新され、正しくユーザー ID が表示される状態となります。これは先程の状態とは異なりますので、再度ブラウザでページをリロードして「ロード中」の画面になるようにしてください

https://zenn.dev/dala/books/nextjs-firebase-service/viewer/user-detail-page

まった | maztak.ethまった | maztak.eth

初回レンダリングの考慮で

if (query.uid !== undefined) {
      return
    }

ってしとったわ。。query.uidがあるのに毎回ここでreturnされちゃうからそりゃうごかんわ。。

まった | maztak.ethまった | maztak.eth

global.cssなどが予め入ってるのは

yarn create next-app myapp

としたからか?

Next.js公式チュートリアルはcreate-next-appとなってるけど、これってリポジトリ名というよりはコマンド名?つまり同じものinstallしてるっぽい?

npx create-next-app myapp --use-npm --example "https://github.com/vercel/next-learn-starter/tree/master/learn-starter"
まった | maztak.ethまった | maztak.eth

user && という条件を入れることで、user が存在する場合のみに囲んだ部分の描画が行われる

[uid].tsx
        {user && (
            <h1>見出し</h1>
        )}
まった | maztak.ethまった | maztak.eth

_app.tsxでimportすると全てのファイルで明示的にimportしなくても使えるようだ(global.scssとか)

まった | maztak.ethまった | maztak.eth

currentUser.uidでは画面をロードした状態でまだ値がないなどでエラーになるので、作成したuseAuthenticationをインポートして取得するuserを使う。

import { useAuthentication } from '../../hooks/authentication'

export default function Home() {
  const { user } = useAuthentication()

  print(user.uid)
  return (<div>hogehoge</div>)
}
まった | maztak.ethまった | maztak.eth

Recoil

A state management library for React.
https://recoiljs.org/docs/introduction/getting-started#atom

Atom

An atom represents a piece of state. Atoms can be read from and written to from any component. Components that read the value of an atom are implicitly subscribed to that atom, so any atom updates will result in a re-render of all components subscribed to that atom:

const textState = atom({
  key: 'textState', // unique ID (with respect to other atoms/selectors)
  default: '', // default value (aka initial value)
});
function TextInput() {
  const [text, setText] = useRecoilState(textState);

  const onChange = (event) => {
    setText(event.target.value);
  };

  return (
    <div>
      <input type="text" value={text} onChange={onChange} />
      <br />
      Echo: {text}
    </div>
  );
}
まった | maztak.ethまった | maztak.eth

07. Recoilでユーザー情報を保存する

const { user } = useAuthentication()で、なぜuserをオブジェクトにして?returnし、また分割代入?でuserだけを受け取っているのかわからない。

export interface User {
  uid: string
  isAnonymous: boolean
  name: string
}
まった | maztak.ethまった | maztak.eth

Firebaseのセキュリティー

こういうルールのためのメソッドがあるのか

request.resource.data.diff(resource.data).affectedKeys().hasOnly(['isReplied'])
このスクラップは2021/12/29にクローズされました