尝试在React中调用函数

时间:2019-10-25 03:12:42

标签: javascript reactjs react-hooks

我有一个名为Toolbar.js的Java脚本文件,在其中试图按以下方式调用App.js文件中的函数:

<DrawerToggleButton click={props.drawerClickHandler}/>

上面的方法不起作用,它显示“ UnresolvedvariableClickClickHandler”,我认为这意味着Toolbar.js在App.js中看不到功能。我尝试了这个,没有使用道具没有结果。我该如何工作?

也只是想声明程序将正确构建并运行,只是我试图将函数映射到的按钮在单击时什么也没做。

以下代码:

Toolbar.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="/dashboard">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;

App.js:

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, setIsAuthenticated] = useState(false);
  const [isAuthenticating, setIsAuthenticating] = useState(true);
  const [sideDrawerOpen, setIsSideDrawerOpen] = useState(false);

  useEffect(() => {
    onLoad();
  }, [isAuthenticating, isAuthenticated]);

  async function onLoad() {
    try {
      await Auth.currentSession();
      setIsAuthenticated(true);
      props.history.push("/dashboard");
    }
    catch(e) {
        alert(e);
    }
    setIsAuthenticating(false);
  }

  function handleLogout() {
    setIsAuthenticated(false);
    props.history.push("/login");
  }

  function drawerToggleClickHandler(){
    setIsSideDrawerOpen(!sideDrawerOpen)
  }

  let sideDrawer;
  let backdrop;
  let toolBar;

  if(isAuthenticated){
    toolBar = (
        <Toolbar
            handleLogout={handleLogout}
            drawerClickHandler ={drawerToggleClickHandler}
        />
    )
  }

  if (isAuthenticated && sideDrawerOpen){
    sideDrawer = <SideDrawer/>;
    backdrop = <Backdrop/>
  }
  return (
    <div className="App container" style={{height: '100%'}}>
      {toolBar}
      {sideDrawer}
      {backdrop}
      {/*<Toolbar/>*/}
      {/*<SideDrawer/>*/}
      {/*<Backdrop/>*/}
      <Routes appProps={{ isAuthenticated, setIsAuthenticated }} />
    </div>
  );
}

export default withRouter(App);

DrawerToggleButton.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;

0 个答案:

没有答案