简体   繁体   English

如何将参数从一个 React 组件导入到另一个组件

[英]How to import parameters from one React component to another

Please apologize my lack of knowledge but I'm new to React and JavaScript.请原谅我缺乏知识,但我是 React 和 JavaScript 的新手。

I'm building a Website where I have a Sidebar and a Navbar on top of the page.我正在建立一个网站,我在页面顶部有一个侧边栏和一个导航栏。 The Sidebar is activated / deactivated via an hamburger menu which is located in the Navbar-component.边栏通过位于导航栏组件中的汉堡菜单激活/停用。 It is activated via CSS-classes: <nav className={isActive? 'nav-menu active': 'nav-menu'}>它通过 CSS 类激活: <nav className={isActive? 'nav-menu active': 'nav-menu'}> <nav className={isActive? 'nav-menu active': 'nav-menu'}>

My Problem now is, that I dont know how to transfer the variables toggleButton and isActive from the Navbar.jsx where they are declared to the Sidebar.jsx where I need them to activate my Sidebar.我现在的问题是,我不知道如何将变量 toggleButton 和 isActive 从声明它们的 Navbar.jsx 传输到需要它们激活侧边栏的 Sidebar.jsx。 Do I need to import the js files?我需要导入js文件吗? Is it possible to export isActive and toggleButton seperatly from the JSX-Code?是否可以从 JSX 代码中单独导出 isActive 和 toggleButton?

Thanks for your help: :)谢谢你的帮助: :)

This is my Code:这是我的代码:

Navbar-component导航栏组件

import React from "react";
import Navbar from "react-bootstrap/Navbar";
import Button from "react-bootstrap/Button";
import Form from "react-bootstrap/Form";
import Nav from "react-bootstrap/Nav";
import FormControl from "react-bootstrap/Form";
import Dropdown from "react-bootstrap/Dropdown";
import './Navbar.css';



    const NavbarTop = () => {
      const [isActive, setIsActive] = useState(false)
    
      const toggleButton = useCallback(
        () => setIsActive(prevState => !prevState),
        [],
      );
      return (
        <>
    
      <Navbar className="background-color" variant="light">
        <Navbar.Brand href="#home">
            <img
                src="https://pbs.twimg.com/profile_images/603568605796634624/Nq0nXZZA_400x400.jpg"
                width="30"
                height="30"
                className="d-inline-block align-top"
                alt="React Bootstrap logo"
            />
            </Navbar.Brand>
            <Dropdown>
                <Dropdown.Toggle Classname="color" rvariant="success" id="dropdown-basic">
                Kategorien
                </Dropdown.Toggle>
    
            <Dropdown.Menu>
                <Dropdown.Item href="#/action-1">Bücher</Dropdown.Item>
                <Dropdown.Item href="#/action-2">Veranstaltungen</Dropdown.Item>
                <Dropdown.Divider />
                <Dropdown.Item href="#/action-3">Etwas Posten</Dropdown.Item>
            </Dropdown.Menu>
            </Dropdown>
        <Form inline>
          <FormControl type="text" placeholder="Search" className="mr-sm-2" />
          <Button variant="outline-primary">Search</Button>
        </Form>
        <Link to='#' className='menu-bars'>
              <HamburgerSpring className="Hamburger"
                buttonColor="transparent"
                barColor="#007466"
                buttonWidth={35}
                {...{ isActive, toggleButton }}
                />
              </Link>
              </Animate>
      </Navbar>
    </>
      );
    };
    
    export default NavbarTop;

Sidebar-component:侧边栏组件:

import React, { useState, useCallback } from 'react';
import { Link } from 'react-router-dom';
import { SidebarData } from './SidebarData';
import { IconContext } from 'react-icons';
import {Animate} from 'react-rebound';
import { HamburgerSpring } from 'react-animated-burgers';
import './Sidebar.css';




function Sidebar(isActive, toggleButton) {
  
 
  return (
          <>
    
      <IconContext.Provider value={{ color: 'white' }}>
          
        <nav className={isActive ? 'nav-menu active' : 'nav-menu'}>
          <div className='nav-menu-items'>
            <ul  onClick={toggleButton}>
              {SidebarData.map((item, index) => {
                return (
                  <li key={index} className={item.cName}>
                    <Link to={item.path}>
                      {item.icon}
                      <span>{item.title}</span>
                    </Link>
                  </li>
                );
              })}
            </ul>
            

          </div>
        </nav>
      
          
    </IconContext.Provider>
    </>
  );
}

export default Sidebar;

One possible approach is to move the state one level up.一种可能的方法是将 state 上移一级。

const ParentComponent = () => {

   const [isActive, setIsActive] = useState(false)
    
   const toggleButton = useCallback(
     () => setIsActive(prevState => !prevState),
      [],
   );
    
   .....
   .....
   {/* You can use like this */}
   <SideBar isActive={isActive} toggleButton={toggleButton} />
    
   <NavBar isActive={isActive} toggleButton={toggleButton} />

 }

Alternatively, you can use the context API to store to hold the state and access it anywhere within the app.或者,您可以使用上下文 API来存储以保存 state 并在应用程序中的任何位置访问它。 In your case, I think the first option is a better idea given it's simple nature.在你的情况下,我认为第一个选项是一个更好的主意,因为它很简单。

This is where you'd lift state up, outside of those into a container of sorts.这是您将 state 提起的地方,将它们放在各种容器中。

function Container() {
    const [toggleIsActive, setToggleIsActive] = useState(false);

    return (
        <Navbar ... toggleIsActive={toggleIsActive} setToggleIsActive={setToggleIsActive} />
        <Sidebar ... toggleIsActive={toggleIsActive} setToggleIsActive={setToggleIsActive} />
    )
}

Essentially you track that from higher up in the tree while providing the values/methods for changing to the children components.本质上,您在树中从较高位置跟踪它,同时提供更改为子组件的值/方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM