【问题标题】:React Native How to get return value from two functionReact Native 如何从两个函数中获取返回值
【发布时间】:2021-06-11 11:29:06
【问题描述】:

我在反应原生类组件中有两个功能。我尝试通过调用 test1() 来获取返回值,但它没有显示返回值。如果直接调用 test2(),我可以得到返回值。但是这两个函数中都会有很多 if else 条件和返回值,所以我需要调用函数 test1( )... 有人可以帮忙吗?

test1(){
   this.test2()
}

test2(){
   return(
     <Text>Testing123</Text>
   )
}

render{
   return(
     <View>
       {this.test1()}
     </<View>
   )
}

【问题讨论】:

标签: javascript react-native


【解决方案1】:

需要在test1中返回test2返回的值。

test1(){
  return(this.test2())
}

test2(){
  return(
     <Text>Testing123</Text>
    )
  }

  render{
    return(
      <View>
        {this.test1()}
      </<View>
      )
  }

我还建议您使用箭头函数而不是普通函数。

const test1 = () => {
   //Code
} 

or

test1 = () => {
   //Code
}

而不是

test1(){
   //Code 
}

问候。

【讨论】:

  • 它有效,谢谢!如果我需要返回2个函数,可以这样写吗? return( this.showRegisterApproval(), this.showEventStatus() )
  • 是的,我猜!取决于你在做什么。如果有帮助,您可以将我的答案标记为勾号吗?谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
  • 1970-01-01
  • 2011-04-04
  • 2021-02-13
相关资源
最近更新 更多