From 228c9e941731433feabdc0f7a698520c2b7aa955 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Wed, 30 Mar 2022 22:36:10 +1100 Subject: [PATCH] Chap06 -> Understanding controlled components --- QandA/frontend/src/Header.tsx | 56 +++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/QandA/frontend/src/Header.tsx b/QandA/frontend/src/Header.tsx index 78b5176..e15af90 100644 --- a/QandA/frontend/src/Header.tsx +++ b/QandA/frontend/src/Header.tsx @@ -1,14 +1,23 @@ /** @jsxImportSource @emotion/react */ import { css } from '@emotion/react'; import React from 'react'; -import { Link } from 'react-router-dom'; +import { Link, useSearchParams } from 'react-router-dom'; import { UserIcon } from './Icons'; import { fontFamily, fontSize, gray1, gray2, gray5 } from './Styles'; export const Header = () => { + const [searchParams] = useSearchParams(); + const criteria = searchParams.get('criteria') || ''; + const [search, setSearch] = React.useState(criteria); + const handleSearchInputChange = (e: React.ChangeEvent) => { - console.log(e.currentTarget.value); + setSearch(e.currentTarget.value); + }; + + const handleSubmit = (e: React.FormEvent) => { + e.preventDefault(); + console.log(search); }; return ( @@ -38,26 +47,29 @@ export const Header = () => { > Q & A - +
+ +