简体   繁体   English

React Native 如何从两个 function 获取返回值

[英]React Native How to get return value from two function

I have two function in react native class component.我在反应原生 class 组件中有两个 function 。 I try to get the return value by calling test1( ), but it do not show out the return value.我尝试通过调用 test1() 来获取返回值,但它没有显示返回值。 I can get the return value if straight call test2( ).如果直接调用 test2(),我可以得到返回值。 But here will have many if else condition and return value in both function, so I need to call the function test1( )... Can someone help?但是这里在 function 中会有很多 if else 条件和返回值,所以我需要调用 function test1( )...有人可以帮忙吗?

test1(){
   this.test2()
}

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

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

You need to return the value returned from test2 in test1.您需要在 test1 中返回从 test2 返回的值。

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

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

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

And I would also advise you to use arrow functions rather than normal functions.我还建议您使用箭头函数而不是普通函数。

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

or

test1 = () => {
   //Code
}

rather than而不是

test1(){
   //Code 
}

Regards.问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM