亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法在反應功能組件中的道具上設置狀態

無法在反應功能組件中的道具上設置狀態

飲歌長嘯 2022-07-08 10:48:36
我一直無法在我不斷得到的道具上設置狀態TypeError:props.setState 不是函數我正在嘗試實現搜索功能const HeroComp = (props) => {        let  handleSearchSubmit = (e) => {          props.setState({searchValue: e.target.value});      }      return     <div className='heroComp' >                <form action="" >                   <input type="text" placeholder='search cartigory'  onChange={handleSearchSubmit}   />                 </form>            </div>}export default HeroComp;當我 console.log(props) 我得到{searchValue: ""}searchValue: ""__proto__: Object這是父組件import images from '../data/images'; //the file from which i'm importing images dataclass HomePage extends React.Component{    constructor(){        super();        this.state = {            images,            searchValue: ''            }}    render(){        const {images , searchValue} = this.state;        const filteredImage = images.filter(image => image.cartigory.toLowerCase().includes(searchValue));            return(                <div >                    <HeroComp searchValue={ searchValue }  />                    <GalleryComp filteredImage={filteredImage} />                </div>            )        }}export default HomePage;我知道這應該很容易,但我只是看不到解決方案。
查看完整描述

2 回答

?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

這個怎么樣?


  useEffect(() => {

    // set the current state

    setSearchValue(props.searchValue)

  }, [props]);


查看完整回答
反對 回復 2022-07-08
?
MYYA

TA貢獻1868條經驗 獲得超4個贊

功能組件沒有狀態,但你可以使用 reactHooks:


import React, { useState } from 'react';


const HeroComp = (props) => {

      let [searchValue, setSearchValue] = useState();


      let  handleSearchSubmit = (e) => {

          setSearchValue(e.target.value);

      }

      return     <div className='heroComp' >

                <form action="" >


                <input type="text" placeholder='search cartigory'  onChange={handleSearchSubmit}   />

                 </form>

            </div>

}



export default HeroComp;


查看完整回答
反對 回復 2022-07-08
  • 2 回答
  • 0 關注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號