# わからない
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/>
あなたの回答
tips
プレビュー