javascript - react : Trying to pass an array to a functional component, 但它不会呈现

标签 javascript reactjs es6-modules

正如标题所说...我可以让控制台回显数组,但它不会呈现在我的导航栏上。

来自 App.js:

unValidatedUserButtons = ["Search", "Login", "Register"];  

render() {
    return (
      <Layout>
          <Header />
          <Topnav buttons={this.unValidatedUserButtons}/>
      </Layout>
    )
}

到顶部导航:

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>
    );   
}

然后到 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>
    )
}

如果有人可以帮助我渲染按钮,将不胜感激。

最佳答案

buttonHandlerAccount 缺少 return声明。

// Button Handler Account.
const buttonHandlerAccount = (newButtons) => {
  return newButtons.map((Button, index) => <button key={index}>{Button}</button>)
}

关于javascript - react : Trying to pass an array to a functional component, 但它不会呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53057129/

相关文章:

javascript - 即使我使用 Number() 函数,数组的最后一个元素始终为 NaN

javascript - 在 Javascript 中导入 Typescript 文件

javascript - 如何将悬停规则添加到 css className 以便在悬停时为 react 表中的特定行着色?

javascript - VSCode 不建议导入 ES6?

javascript - jQuery.getJSON() 如何获取请求的状态

javascript - 如何在 JSX 中添加三元运算符?

reactjs - 在 ReactJS 中管理大量表单输入字段的专业方法是什么?

node.js - 如何在 Node.js 中使用 ES6 导入?

node.js - ESLint 规则在用户模块和标准 Node 模块之间插入一个空行

javascript - firepad 在 javascript 中改变高度