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

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

React 狀態值在傳遞給模型時變為 null

React 狀態值在傳遞給模型時變為 null

慕勒3428872 2022-05-14 14:40:04
我有 AboutMeText 狀態并且它通過道具成功更新,我成功顯示了它的值<h6>{this.state.AboutMeText}</h6>,但是當我嘗試將此狀態值傳遞給模型時,它的值變為空。我已經嘗試過 {this.state.AboutMeText}或{this.props.AboutMeText}沒有工作請幫助我先生,我是 React js 的新手 <DIV className="template-profile">   <Image     url={this.state.selectedImages.profilePicture}     mode={"Profile"}   />   <h6>Hello I'm</h6>   <h1 className="template-profile-name">     Nabnit Jha   </h1>   <h6>     {this.state.AboutMeText} // here state value display successfully   </h6>   <DIV className="template-self-info">     <Modals modalBody={this.state.AboutMeText} modalHeading="About Me"></Modals> // here state value became null   </DIV> </DIV>
查看完整描述

2 回答

?
元芳怎么了

TA貢獻1798條經驗 獲得超7個贊

如果我正確理解您的問題。您需要設置默認狀態以避免空數據


如果在類的構造函數中使用類組件,則應設置狀態的默認值


class SomeComponent extends React.Component {

   constructor(props) {

    super(props);

    this.state = {

         someData: []

    };

   }

}

如果你使用功能組件,你應該使用反應鉤子


const SomeComponent = () => {

   const [items, setItems] = useState([]); // default value empty array

};


查看完整回答
反對 回復 2022-05-14
?
一只萌萌小番薯

TA貢獻1795條經驗 獲得超7個贊

根據以下條件打開您的模型 -


               <DIV className="template-profile">

                    <Image

                        url={this.state.selectedImages.profilePicture}

                        mode={"Profile"}

                    />

                    <h6>Hello I'm</h6>

                    <h1 className="template-profile-name">

                        Nabnit Jha

                    </h1>

                    <h6>

                    {this.state.AboutMeText} // here state value display successfully

                    </h6>

                    <DIV className="template-self-info">

                        {modelStatus&&<Modals modalBody={this.state.AboutMeText} modalHeading="About Me"></Modals>}

                        //here state value became null

                    </DIV>

                </DIV>

實現它的功能


功能clickHandle


clickHandle=()=>{

  this.setState({modelStatus:true})

}

您根據您的要求使用您的功能。


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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