简体   繁体   English

反应this.props.children.apply不是一个函数

[英]React this.props.children.apply is not a function

I'm making a react app with unstated to manage the state of my app, the application is a simple CRUD of notes. 我正在使用一个未声明状态的React应用程序来管理我的应用程序状态,该应用程序是一个简单的注释注释。

I have a NoteList component to render the list of Notes. 我有一个NoteList组件来呈现Notes列表。 This is my render() function in NoteList Component: 这是我在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>
        )
}

However, when i load the page, i get the following error: 但是,当我加载页面时,出现以下错误:

this6.props.children.apply is not a function this6.props.children.apply不是函数

And in the chrome console the next description: 并在chrome控制台中进行以下说明:

The above error occurred in the component: in Consumer (created by Subscribe) in Subscribe (at NoteList.js:19) in NoteList (at App.js:160) in Provider (created by Consumer) in Consumer (created by Provider) in Provider (at App.js:159) in App (at src/index.js:7) 上面的错误发生在组件中:消费者(由订阅创建)在订阅者(由NoteList.js:19创建)中的订阅者(由App.js:160创建)在提供者(由消费者创建)的提供者中(由App.js:160创建) App(src / index.js:7)中的提供者(App.js:159)

I have no clue on what could be the source of this error, if anyone can give me any help i would appreciate it! 我不知道此错误的根源是什么,如果有人可以给我任何帮助,我将不胜感激!

Thanks in advance, have a nice day! 在此先感谢您,祝您度过愉快的一天!

The children of <Subscribe /> is a function that returns array of childrens, thats why the error. <Subscribe />的子代是一个返回子代数组的函数,这就是为什么会出错。 To check it, inside <Subscribe /> , you could do console.log(this.props.children) and you will see that it's an array, you can't use apply in an array, you need to loop through and apply it to each children 要检查它,可以在<Subscribe />执行console.log(this.props.children) ,您会看到它是一个数组,不能在数组中使用apply,需要遍历并应用它给每个孩子

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

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

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