我是 React 的新手,并嘗試根據狀態變量加載不同的組件。我希望我的處理程序動態地知道我正在更新哪個狀態變量。到目前為止,我明確地將狀態名稱作為字符串傳遞。狀態變量state = { showLogin: false, showHome: false }處理方法handleShow = (element) => this.setState({ element: true });紐扣{ !this.props.isAuthenticated ? ( <Button variant="outline-primary" onClick={() => this.handleShow("showLogin")} > Login </Button> ) : ( <> <Button variant="outline-primary" onClick={() => this.handleShow("showHome")} > Home </Button> <Button variant="outline-primary" onClick={this.authorizeUserMethod}> LogOut </Button> </> );}
單擊更新狀態變量以顯示不同的組件
繁星coding
2022-05-26 14:20:39