简体   繁体   English

为什么两种代码在本机代码中出现不同的行为?

[英]Why is the two kinds of code appear different behavior in react-native?

I write this two kinds of code, the first one it appear correct render, but the second is appear incorrect empty content render, I don't know why? 我编写了这两种代码,第一个代码显示正确的渲染,但是第二个代码显示不正确的空内容渲染,我不知道为什么? Is there any one can explain the behavior? 有没有人可以解释这种行为?

First 第一

import React from 'react'
import { Text } from 'react-native'

const Header = () => {
  return <Text style={style.header}>{content}</Text>;
}

const style = {
 header: {
  fontSize: 20
 }
}

const content = "content"

export { Header };

Second 第二

import React from 'react'
import { Text } from 'react-native'

const Header = () => {
  return <Text style={style.header}>{content}</Text>;
}

const style = {
 header: {
  fontSize: 20
 }
}

const content = () => {
  return "content"
}

export { Header };

In the second case you're defining content as a function. 在第二种情况下,您将content定义为函数。 You should call it: 您应该称呼它为:

  return <Text style={style.header}>{content()}</Text>;

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

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