質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

0回答

784閲覧

[React] inputフォームで不明なWarningが出る

DaisukeMori

総合スコア226

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2020/11/02 15:12

# わからない
Reactでinputフォームを使った際下記のようにコードを書くと不明なWarningが出ます。
これの意味が知りたいです。

コード

js

1import React, { useState, useEffect, useContext } from 'react'; 2import { useHistory } from 'react-router-dom'; 3import { Link } from 'react-router-dom'; 4import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; 5import { faSearch } from '@fortawesome/free-solid-svg-icons'; 6import Style from './Header.module.scss'; 7import { Store } from '../../store/'; 8 9const Header = () => { 10 const [term, setTerm] = useState(''); 11 const history = useHistory(); 12 const { globalState, setGlobalState } = useContext(Store); 13 const handleSubmit = e => { 14 e.preventDefault(); 15 setGlobalState({ type: 'SET_TERM', payload: { term } }); 16 history.push(`search?query=${term}`); 17 } 18 useEffect(() => { 19 setTerm(globalState.term); 20 // eslint-disable-next-line react-hooks/exhaustive-deps 21 }, []); 22 23 return ( 24 <div className={ Style.header }> 25 <div className={ Style.item }> 26 <Link to="/">ReactTube</Link> 27 </div> 28 <div className={ Style.item }> 29 <form onSubmit={ handleSubmit }> 30 <input 31 type="text" 32 placeholder="検索" 33 onChange={ e => setTerm(e.target.value) } 34 value={ term } 35 /> 36 <button type="submit"> 37 <FontAwesomeIcon icon={faSearch} /> 38 </button> 39 </form> 40 </div> 41 </div> 42 ); 43} 44 45export default Header;

Warning

bash

10.chunk.js:46488 Warning: A component is changing a controlled input to be uncontrolled. This is likely caused by the value changing from a defined to undefined, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://reactjs.org/link/controlled-components 2 at input 3 at form 4 at div 5 at div 6 at Header (http://localhost:3000/static/js/main.chunk.js:437:81) 7 at div 8 at Layout (http://localhost:3000/static/js/main.chunk.js:724:3) 9 at Top (http://localhost:3000/static/js/main.chunk.js:2278:63) 10 at Route (http://localhost:3000/static/js/0.chunk.js:49165:29) 11 at Switch (http://localhost:3000/static/js/0.chunk.js:49367:29) 12 at Router (http://localhost:3000/static/js/0.chunk.js:48800:30) 13 at BrowserRouter (http://localhost:3000/static/js/0.chunk.js:48420:35) 14 at div 15 at App 16 at StoreProvider (http://localhost:3000/static/js/main.chunk.js:2750:3)

ちなみに以下を消すとWarningが消えましたが、こちらで解決してるのかどうかわからないので、かなり不安です。

js

1<input 2 type="text" 3 placeholder="検索" 4 onChange={ e => setTerm(e.target.value) } 5 // value={ term } // ここを消す 6/>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問