gpt4 book ai didi

javascript - react 功能没有从功能组件传递到功能组件

转载 作者:行者123 更新时间:2023-12-04 10:04:58 25 4
gpt4 key购买 nike

我非常习惯于对组件进行分类,现在正试图更习惯于钩子(Hook),所以我正在深入研究函数式组件。

遇到一个棘手的问题,因为我无法将一个功能从一个功能组件传递给它的功能组件子组件。

在父组件中:

import React, { useState, useEffect } from 'react';
import { Link } from 'gatsby';
import { MenuList, MenuItem } from '@material-ui/core';
import { withCookies, Cookies } from 'react-cookie';
import { ExpandMore } from '@material-ui/icons';
import PropTypes from 'prop-types';
import styles from '../styles/nav.module.scss';
import NavDrawer from './navDrawer';

const activeStyle = {
color: '#445565',
backgroundColor: '#de1b',
borderColor: '#ced4da',
};

const NavMobile = (props) => {
const [isOpenProductsMenu, setIsOpenProductsMenu] = useState(false);
const [isOpenServicesMenu, setIsOpenServicesMenu] = useState(false);
const [isAdmin, setIsAdmin] = useState(false);

const { cookies, categories } = props;
const main = process.env.ROOT_CATEGORIES.split(',');
useEffect(() => {
if (cookies.get('roles')) {
const roles = cookies.get('roles');
setIsAdmin(!!roles.includes('ROLE_ADMIN') || roles.includes('ROLE_SUPERADMIN'));
}
});
const toggleProductsMenu = (isOpen) => {
if (event.type === 'keydown' && (event.key === 'Tab' || event.key === 'Shift')) {
return;
}
setIsOpenProductsMenu(isOpen);
};
const toggleServicesMenu = (isOpen) => {
if (event.type === 'keydown' && (event.key === 'Tab' || event.key === 'Shift')) {
return;
}
setIsOpenServicesMenu(isOpen);
};
return (
<MenuList className={styles.navMobile} style={{ padding: '0 10px 0 0' }}>
<MenuItem disableGutters>
<Link to="/" activeStyle={activeStyle}>Home</Link>
</MenuItem>
<MenuItem disableGutters>
<Link to="/search" activeStyle={activeStyle}>Search</Link>
</MenuItem>
<MenuItem
onMouseEnter={() => toggleProductsMenu(true)}
onMouseLeave={() => toggleProductsMenu(false)}
className={styles.overrideItem}
disableGutters
>
<NavDrawer
type={main[0]}
open={isOpenProductsMenu}
categories={categories}
toggleMenu={toggleProductsMenu}
/>
<Link to="/" onClick={(event) => event.preventDefault}>
<div className={styles.itemWrap}>
{main[0]}
<ExpandMore />
</div>
</Link>
</MenuItem>
<MenuItem
onMouseEnter={() => toggleServicesMenu(true)}
onMouseLeave={() => toggleServicesMenu(false)}
className={styles.overrideItem}
disableGutters
>
<NavDrawer
type={main[1]}
open={isOpenServicesMenu}
categories={categories}
toggleMenu={toggleServicesMenu}
/>
<Link to="/" onClick={(event) => event.preventDefault}>
<div className={styles.itemWrap}>
{main[1]}
<ExpandMore />
</div>
</Link>
</MenuItem>
{isAdmin ?
(
<MenuItem disableGutters>
<Link to="/admin" activeStyle={activeStyle}>Admin</Link>
</MenuItem>
) : null}
</MenuList>
);
};

NavMobile.propTypes = {
cookies: PropTypes.instanceOf(Cookies).isRequired,
categories: PropTypes.array.isRequired,
};

export default withCookies(NavMobile);

但是,如果我尝试访问子组件中的 Prop ,如下所示, toggleMenu函数不会在输出中;

而且,当然,会抛出 type error .
export default function NavDrawer(props) {
const {
type,
open,
categories,
toggleMenu,
} = props;
console.log('props drawer', props);

<Drawer
id={`parentDrawer-${type}`}
onMouseEnter={() => setIsOpenSubDrawer(true)}
onMouseLeave={() => setIsOpenSubDrawer(false)}
className={classes.drawer}
variant="temporary"
open={open}
close={toggleMenu(false)}

console output

我错过了什么?

最佳答案

toggleProductsMenu 和 toggleServicesMenu 中的事件对象未定义。

关于javascript - react 功能没有从功能组件传递到功能组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61632350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com