【问题标题】:How to pass props to component inside function?如何将道具传递给函数内部的组件?
【发布时间】:2020-05-28 08:03:08
【问题描述】:

当我尝试将 props 传递给函数内的 Component 时,它总是返回“期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions”。

handleComponent(){
     <Component getid={value} />
}

为什么会发生这种情况以及如何纠正它?

【问题讨论】:

    标签: reactjs react-native


    【解决方案1】:

    你必须调用组件以返回像这样的组件中的渲染

     render(){
         return(
           <Component getid={value}/>
         )
        }
    

    【讨论】:

    • @user12551649 如果这对您的问题有影响,请按照标记进行,可以关闭此问题。
    【解决方案2】:

    你在渲染函数中使用了handleComponent(或者如果你使用功能组件作为回报),我猜,所以handleComponent需要返回一段JSX代码。你的handleComponent没有return关键字所以它return undefined,这就是你得到那个错误的原因。 试试下面的代码:

    handleComponent(){
         return <Component getid={value} />
    }

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 2017-05-29
      • 2017-10-31
      • 2018-08-01
      • 2019-07-03
      • 2018-04-28
      • 1970-01-01
      • 2018-12-17
      • 1970-01-01
      相关资源
      最近更新 更多