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

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

狀態改變后條件渲染不改變

狀態改變后條件渲染不改變

墨色風雨 2021-06-15 17:01:35
我是 React 的新手,我正在嘗試在無狀態子組件中執行條件渲染。當我更新我的狀態以強制改變條件時,什么也沒有發生。我已經檢查以確保我的回調函數實際上正在改變它的狀態。我已經確保道具正常工作,因為我能夠通過道具從孩子那里檢索其他數據。//main app componentthis.state={FileViewable: false} changeViewStatetoTrue = () =>{   this.setState({FileViewable:!this.state.FileViewable}, function (){     console.log(this.state.FileViewable)   });//there are more routes but I just wanted to include the route in question  <Switch>        <Route path="/app" render={props=><Body {...props} changeViewStatetoTrue={this.changeViewStatetoTrue} fileviewableState={this.FileViewable}/>}/>      </Switch>//Body component where the conditional rendering should happen, there is a Tesseract OCR API call being made here, i've left it out because it works fine  render(props) {      var documentView;      if(!this.props.fileviewableState){      documentView =   <div> view number 1 </div>      } else  {      documentView =  <div>          <h1>view number 2 </h1>             </div>      }我沒有看到任何錯誤消息和狀態正在更改的控制臺日志。我不知道為什么會這樣?
查看完整描述

2 回答

?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

可能的錯誤在這里:


  <Switch>

      <Route path="/app" render={props=><Body {...props} changeViewStatetoTrue={this.changeViewStatetoTrue} fileviewableState={this.FileViewable}/>}/>

  </Switch>

應該:


<Switch>

    <Route path="/app" render={props=><Body {...props} changeViewStatetoTrue={this.changeViewStatetoTrue} fileviewableState={this.state.FileViewable}/>}/>

</Switch>

(fileviewableState={this.FileViewable}應該是fileviewableState={this.state.FileViewable}


查看完整回答
反對 回復 2021-06-24
?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

嘗試改變:

如果(!this.props.fileviewableState)

至:

if(!this.state.FileViewable)


查看完整回答
反對 回復 2021-06-24
  • 2 回答
  • 0 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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