Javascript React:试图将数组传递给功能组件,但失败了';不渲染
正如标题所说。。。我可以让控制台回显阵列,但它不会呈现在我的导航栏上 从App.js:Javascript React:试图将数组传递给功能组件,但失败了';不渲染,javascript,reactjs,es6-modules,Javascript,Reactjs,Es6 Modules,正如标题所说。。。我可以让控制台回显阵列,但它不会呈现在我的导航栏上 从App.js: unValidatedUserButtons = ["Search", "Login", "Register"]; render() { return ( <Layout> <Header /> <Topnav buttons={this.unValidatedUserButtons}/> <
unValidatedUserButtons = ["Search", "Login", "Register"];
render() {
return (
<Layout>
<Header />
<Topnav buttons={this.unValidatedUserButtons}/>
</Layout>
)
}
unValidatedUserButtons=[“搜索”、“登录”、“注册”];
render(){
返回(
)
}
至TopNav:
buttonHandlerTnav = (buttonPass) => {
let newButtons = [...buttonPass];
this.setState({buttons: newButtons});
this.setState({needButtons: false});
}
render(){
if (this.state.needButtons)
this.buttonHandlerTnav(this.props.buttons);
return (
<div className={classes.TopNav}>
<MenuButton />
<AccountButtons
buttonPass={this.state.buttons} />
</div>
);
}
buttonHandlerTnav=(buttonPass)=>{
让newButtons=[…buttonPass];
this.setState({buttons:newButtons});
this.setState({needButtons:false});
}
render(){
if(this.state.needButtons)
this.buttonHandlerTnav(this.props.buttons);
返回(
);
}
然后转到AccountButtons:
const buttonHandlerAccount = (newButtons) => { newButtons.map((Button, index) => {
return (<button key={index}>{Button}</button>)
});
}
const accountButtons = (props) => {
// console.log(props.buttonPass);
return (
<div className={classes.AccountButtons}>
{buttonHandlerAccount(props.buttonPass)}
</div>
)
}
const buttonHandlerAccount=(newButtons)=>{newButtons.map((按钮,索引)=>{
返回({Button})
});
}
常量accountButtons=(道具)=>{
//控制台日志(道具按钮程序);
返回(
{buttonHandlerAccount(props.buttonPass)}
)
}
如果有人能帮我绘制按钮,我将不胜感激。buttonHandlerAccount缺少一条语句
// Button Handler Account.
const buttonHandlerAccount = (newButtons) => {
return newButtons.map((Button, index) => <button key={index}>{Button}</button>)
}
//按钮处理程序帐户。
常量按钮HandlerAccount=(新按钮)=>{
返回newButtons.map((按钮,索引)=>{Button})
}
您的功能组件中似乎缺少一个返回值。尝试以下方法解决您的问题:
const buttonHandlerAccount = (newButtons) => {
return newButtons.map((Button, index) => {
return (<button key={index}>{Button}</button>)
});
}
const buttonHandlerAccount=(newButtons)=>{
返回newButtons.map((按钮,索引)=>{
返回({Button})
});
}
或者,简称为:
const buttonHandlerAccount = (newButtons) => (newButtons.map((Button, index) => {
return (<button key={index}>{Button}</button>)
}))
const buttonHandlerAccount=(newButtons)=>(newButtons.map((按钮,索引)=>{
返回({Button})
}))
是否有理由将按钮存储在状态
的TopNav
中,而不是将其向下传递?还有,为什么要在渲染中执行此操作?