简体   繁体   English

道具没有在反应中传递子组件

[英]Props is not passing in child component in react

I am learning react.我正在学习反应。 I am trying to pass props to the component.我正在尝试将道具传递给组件。 However, it seems like the props are not received correctly by the child.但是,孩子似乎没有正确收到道具。

My App Class is like,我的应用程序 Class 就像,

import React from 'react'
import ImageList from './ImageList'


class App extends React.Component{
    state = { images: [] }

    render = () => {
        return(
            <div className="ui container">
                <ImageList images={this.state.images}/>
            </div>
        )
    }
}

export default App

And my ImageList Class is like我的 ImageList Class 就像

import React from 'react'

class ImageList extends React.Component{
    render = (props) => {
        console.log(props)
        return(
            <div>
                test
            </div>
        )
    }
}

export default ImageList

As you can see I am passing <ImageList images={this.state.images}/> from App.如您所见,我正在从 App 传递<ImageList images={this.state.images}/> This should pass the images state which is an empty array right now.这应该传递图像 state ,它现在是一个空数组。 However, console.log(props) of ImageList is showing undefined but I expect to see an empty array somewhere there(props.images).但是,ImageList 的console.log(props)显示undefined ,但我希望在某处看到一个空数组(props.images)。

In a class component, render doesn't take a props argument.在 class 组件中, render不采用 props 参数。 You reference it with this.props instead.你用this.props来引用它。

In a class component, render doesn't take a props argument.在 class 组件中,render 不采用 props 参数。 You can try to access props like this in the class component.您可以尝试在 class 组件中访问这样的道具。

import React from 'react'

class ImageList extends React.Component{
    render () {
        console.log(this.props)
        return(
            <div>
                test
            </div>
        )
    }
}

export default ImageList

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

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