仍在学习Java和函数。在写一些代码,但是很困惑。
在App.js类中,在return语句中有一行代码,如下所示:
<DrawerToggleButton click={props.drawerClickHandler}/>
在我的工具栏类中,它将通过“点击”激活,如下所示:
<button className="toggle-button" onClick={props.click}>
在以下代码中最后将哪个连接到另一个类DrawerToggleButton:
import React, { useState, useEffect } from "react";
// import logo from './logo.svg';
import './App.css';
import Routes from "./Routes";
import { Auth } from "aws-amplify";
import { Link, withRouter } from "react-router-dom";
// import { Navbar } from "react-bootstrap";
import Toolbar from './components/Toolbar/Toolbar';
import SideDrawer from './components/SideDrawer/SideDrawer';
import Backdrop from './components/Backdrop/Backdrop';
function App(props) {
const [isAuthenticated, userHasAuthenticated] = useState(false);
const [isAuthenticating, setIsAuthenticating] = useState(true);
const [sideDrawerOpen, setIsSideDrawerOpen] = useState(false);
useEffect(() => {
onLoad();
}, []);
async function onLoad() {
try {
await Auth.currentSession();
userHasAuthenticated(true);
}
catch(e) {
if (e !== 'No current user') {
alert(e);
}
}
setIsAuthenticating(false);
}
function handleLogout() {
userHasAuthenticated(false);
props.history.push("/login");
}
function drawerToggleClickHandler(){
if(sideDrawerOpen){
return {setIsSideDrawerOpen: false};
}
else{
return {setIsSideDrawerOpen: true};
}
}
let sideDrawer;
let backdrop;
let toolBar;
if(userHasAuthenticated === true){
toolBar = <Toolbar drawerClickHandler ={props.drawerToggleClickHandler}/>
}
if (userHasAuthenticated === true && sideDrawerOpen === true){
sideDrawer = <SideDrawer/>;
backdrop = <Backdrop/>
}
return (
<div className="App container" style={{height: '100%'}}>
{toolBar}
{sideDrawer}
{backdrop}
{/*<Toolbar/>*/}
{/*<SideDrawer/>*/}
{/*<Backdrop/>*/}
<Routes appProps={{ isAuthenticated, userHasAuthenticated }} />
</div>
);
}
export default withRouter(App);
Toolbas类和DrawerToggleButton传递都没有问题,但是props.drawerToggleClickHandler不起作用,“未解决的变量抽屉ToggleClickHandler”说。
我该如何解决?完整代码如下:
App.js:
import React from 'react';
import './Toolbar.css';
import DrawerToggleButton from '../SideDrawer/DrawerToggleButton'
import '../SideDrawer/DrawerToggleButton';
import sideDrawer from "../SideDrawer/SideDrawer";
const toolbar = props =>(
<header className="toolbar">
<nav className="toolbar_navigation">
<div>
<DrawerToggleButton click={props.drawerClickHandler}/>
</div>
<div className="toolbar_logo"><a href="/">Kleen Portal</a></div>
<div className="spacer" />
<div className="toolbar_navigation-items">
<ul>
<li><a href="/">Logout</a></li>
</ul>
</div>
</nav>
</header>
);
export default toolbar;
Toolbar.js:
import React from 'react';
import './DrawerToggleButton.css'
const drawerToggleButton = props => (
<button className="toggle-button" onClick={props.click}>
<div className="toggle-button_line" />
<div className="toggle-button_line" />
<div className="toggle-button_line" />
</button>
);
export default drawerToggleButton;
DrawerToggleButton.js:
{{1}}
答案 0 :(得分:1)
您没有更改状态:您需要将功能更改为此(我建议将setState
重命名为setsIsSideDrawerOpen
)
function drawerToggleClickHandler(){
if(sideDrawerOpen){
setState(false)
}
else{
setState(true)
}
}
更改
toolBar = <Toolbar drawerClickHandler ={props.drawerToggleClickHandler}/>
到
toolBar = <Toolbar drawerClickHandler ={drawerToggleClickHandler}/>
drawerToggleClickHandler
不是道具
[而且app.js
也不是类,而是功能组件]
回答您的第二个评论问题,我不会事件将您的组件分配给变量,只需内联即可:
<div className="App container" style={{height: '100%'}}>
{userHasAuthenticated && <Toolbar drawerClickHandler =props.drawerToggleClickHandler}/>
}
...