Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 为什么我会收到警告:函数作为子函数无效?_Reactjs_React Native - Fatal编程技术网

Reactjs 为什么我会收到警告:函数作为子函数无效?

Reactjs 为什么我会收到警告:函数作为子函数无效?,reactjs,react-native,Reactjs,React Native,当我尝试调用一个返回map方法结果的方法时,我得到以下错误警告:函数作为子函数无效。如果您返回组件而不是从render返回组件,则可能会发生这种情况,但我不知道原因。这是我的密码: renderAlbums() { return this.state.albums.map(album => <Text>{ album.title }</Text>); } render() { return (

当我尝试调用一个返回map方法结果的方法时,我得到以下错误
警告:函数作为子函数无效。如果您返回组件而不是从render
返回组件,则可能会发生这种情况,但我不知道原因。这是我的密码:

renderAlbums() {
        return this.state.albums.map(album => <Text>{ album.title }</Text>);
    }

    render() {
        return (
            <View>
                { this.renderAlbums }
            </View>
        );
    }
我想知道为什么它不起作用。当我在这里调用render
renderAlbums()
方法时,
{this.renderAlbums}


更奇怪的是,当我试图调用
renderAlbums()
像这样的
{this.renderAlbums()}
加上括号时,它就工作了。

this.renderAlbums
指的是实际的函数本身,而
this.renderAlbums()
实际执行函数,因此表示函数的返回值

警告:函数作为子函数无效。如果返回组件而不是从渲染返回组件,则可能会发生这种情况

基本上,React期望渲染它

在当前脚本中,
this.renderAlbums
是一个函数引用,它不返回任何.function本身不react元素。因此,react无法呈现
this.renderAlbums

<View>
   { this.renderAlbums }
</View>

{this.renderAlbums}
正确的方法:

<View>
   { this.renderAlbums() } //it will return react element which can be rendered.
</View>

{this.renderAlbums()}//它将返回可以呈现的react元素。

您不是在执行函数,只是在提供引用
this.renderAlbums()
将执行它谢谢你,你帮助我更好地理解了ynderstand我在React Native中也遇到了同样的错误,我调用了一个函数,该函数返回另一个函数,该函数返回一个组件。例如,在最近的一个Udacity项目中,当我应该调用{getMetricMetaInfo('bike').getIcon}时,我调用了{getMetricMetaInfo('bike').getIcon()}。请注意第二个函数上的第二组括号以执行该函数。
<View>
   { this.renderAlbums() } //it will return react element which can be rendered.
</View>