const Home =()=>{
const [keyword,setKeyword] = useState('man')
const [list,setList]=useState([])
const [loading,setLoading]=useState(true)
const inputEl = useRef(null)
// console.log(keyword)
const List = ({keyword})=>{
useEffect(()=>{
fetch(`https://www.omdbapi.com/?s=${keyword}&apikey=4a3b711b`)
.then(res=>res.json())
.then(data=>{
setLoading(false)
setList(data.Search)
// console.log(data.Search)
})
.catch(err=>{alert(err)})
},[keyword])
if(loading==true){
return (
<div>
<p>loading...</p>
</div>
)
}
return (
<div>
<p>获取到了关于{keyword}的信息</p>
{
list.map((item,index)=><li key={index}>{1}</li>)
}
</div>
)
}
function handlePress(e){
if(e.key==='Enter'){
setKeyword(inputEl.current.value)
}
}
return(
<div>
<h3>Search engine</h3>
<div>
<input ref={inputEl} type="text" onKeyPress={(e)=>handlePress(e)}/>
</div>
<List keyword={keyword}/>
</div>
)
};
export default Home;
把List组件放到外边去试试