【问题标题】:React this.props.children.apply is not a function反应 this.props.children.apply 不是一个函数
【发布时间】:2019-02-05 11:58:20
【问题描述】:

我正在制作一个带有 unstated 的 React 应用程序来管理我的应用程序的状态,该应用程序是一个简单的注释 CRUD。

我有一个 NoteList 组件来呈现笔记列表。 这是我在 NoteList 组件中的 render() 函数:

render() {
        return (
            <Subscribe to={[NoteContainer]}>
                {noteContainer => (
                    noteContainer.state.isLoaded ?
                        (
                            noteContainer.state.notas.map((note, i) => {
                                return (<Note note={note} index={i} onRemoveNote={this.removeNote} onEditNote={this.saveEditNote} key={i}></Note>)
                            })
                        )
                        :
                        (
                            <div>Loading Notes...</div>
                        )
                    )}
                )}
            </Subscribe>
        )
}

但是,当我加载页面时,我收到以下错误:

this6.props.children.apply 不是函数

并在 chrome 控制台中进行下一个描述:

组件出现上述错误: 在消费者中(由订阅创建) 在订阅中(在 NoteList.js:19) 在 NoteList (在 App.js:160) 在提供者(由消费者创建) 在消费者中(由提供者创建) 在提供者中(在 App.js:159) 在应用程序中(在 src/index.js:7)

我不知道这个错误的根源是什么,如果有人能给我任何帮助,我将不胜感激!

提前致谢,祝你有美好的一天!

【问题讨论】:

  • &lt;Subscribe /&gt; 的孩子是一个返回孩子的函数,我认为这就是错误的原因
  • 订阅您的自定义组件吗?如果可以,请分享一下
  • @Vencovsky,也许,我正在尝试解决方法。
  • @ShubhamKhatri,不,订阅是一个自定义的未声明组件
  • 你的Subscribe组件里面有什么?

标签: javascript reactjs unstated


【解决方案1】:

&lt;Subscribe /&gt; 的孩子是一个返回孩子数组的函数,这就是错误的原因。 要检查它,在&lt;Subscribe /&gt; 中,你可以做console.log(this.props.children),你会看到它是一个数组,你不能在数组中使用apply,你需要循环并将它应用到每个孩子

this.props.children.map(children => children.apply())

【讨论】:

  • 您的回答给了我解决问题的线索,我决定将渲染笔记的逻辑移至另一个函数。 return ( {noteContainer => ( noteContainer.showNotas() )} )
猜你喜欢
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-29
  • 1970-01-01
  • 2020-09-16
  • 1970-01-01
相关资源
最近更新 更多