javascript - 你如何一起传递多个包装好的道具
问题描述
当我尝试以下操作时,我收到此错误:
Unhandled Runtime Error
TypeError: navbar.makeButtonClick is not a function
Source
.next/static/chunks/pages/index.js (19:29) @ onClick
17 | return(
18 | <button href='#' className = {`${styleNavbar.menu__link} ${styleNavbar.r_link} ${styleNavbar.text_underlined}`}{...props}
> 19 | onClick={()=>{navbar.makeButtonClick(!navbar.buttonClick);
| ^
20 | userprops.makeRefresh(!userprops.refresh);
21 | }}>
22 | {navprops.navbutton}
但我真的不明白..makeButtonClick
是一个选项,我使用相同的方法通过mainprops
并且userprops
它有效...
整个组件:
import styleNavbar from '../../../styles/Main/Navbar.module.css'
import React, {useState, useEffect, useRef} from 'react';
const NavButtonCreator = (props) => {
const {mainprops,userprops,navbar,navprops,...styleprops} = props;
return(
<button href='#' className = {`${styleNavbar.menu__link} ${styleNavbar.r_link} ${styleNavbar.text_underlined}`}{...props}
onClick={()=>{navbar.makeButtonClick(!navbar.buttonClick);
userprops.makeRefresh(!userprops.refresh);
}}>
{navprops.navbutton}
</button>
);
};
const NavButtonStyler = (props) => {
const {mainprops,userprops,navbar,...navprops} = props;
return(
<>
{props.place=='left'&&
<div className=''>
<NavButtonCreator mainprops={mainprops} userprops={userprops} navbar={navbar} navprops={navprops} />
</div>}
{props.place=='center'&&
<div className='absolute left-1/2 transform -translate-x-1/2 '>
<NavButtonCreator mainprops={mainprops} userprops={userprops} navbar={navbar} navprops={navprops} />
</div>}
{props.place=='right'&&
<div className='absolute right-0'>
<NavButtonCreator mainprops={mainprops} userprops={userprops} navbar={navbar} navprops={navprops} />
</div>}
</>
)
};
const Navbar = (props) => {
const {mainprops, ...userprops} = props;
const [buttonClick, makeButtonClick] = useState(false);
const navbar = {
buttonClick,
makeButtonClick
};
useEffect(()=>{
return(
()=>{ userprops.setPageId(navbutton);
}
);
},[buttonClick]);
if (userprops.visitorType == 'viewer') {
return(
<div>
<nav className = {`${styleNavbar.page__menu} ${styleNavbar.page__custom_settings} ${styleNavbar.menu}`} >
<ul className = {`${styleNavbar.menu__list} ${styleNavbar.r_list}`} >
<NavButtonStyler mainprops={mainprops} userprops={userprops} {...navbar} place='left' navbutton='the world' />
<NavButtonStyler mainprops={mainprops} userprops={userprops} {...navbar} place='left' navbutton='the country' />
<NavButtonStyler mainprops={mainprops} userprops={userprops} {...navbar} place='left' navbutton='the state' />
<NavButtonStyler mainprops={mainprops} userprops={userprops} {...navbar} place='center' navbutton='the city' />
<NavButtonStyler mainprops={mainprops} userprops={userprops} {...navbar} place='right' navbutton='the town' />
</ul>
</nav>
</div>
)}
};
export default Navbar;
解决方案
在每个NavButtonStyler
组件中(在 内部Navbar
),您都在拆包navbar
道具:
<NavButtonStyler mainprops={mainprops} userprops={userprops} {...navbar} place='left' navbutton='the world' />
^ Here
这意味着buttonClick
andmakeButtonClick
函数作为 props 传递。直接传navbar
道具就好了。
<NavButtonStyler mainprops={mainprops} userprops={userprops} navbar={navbar} place='left' navbutton='the world' />
推荐阅读
- javascript - typescript 或 JavaScript 将嵌套的对象数组转换为键值对
- android - android中的qml文件对话框路径
- angular - 结合界面设置mat-select字段
- swift - 检测人脸或图像时,应该为 ARCoachingOverlayView 选择哪个目标?
- azure-cosmosdb - Gremlin:编写多重遍历最有效的方法是什么?
- php - php更新查询不更新数据(php/mysql)
- html-framework-7 - 入门框架7中的屏幕是空白的
- elasticsearch - 如何在elasticsearch中查询父子关系
- jenkins-pipeline - 如何在通过参数和标签选项选择的节点上运行 jenkins 声明性管道
- centos7 - Cent OS 7 NTP 客户端未通过 NTP 服务器的 IP 地址同步