繁体   English   中英

反应无法设置未定义道具的属性

[英]react cannot set property of props of undefined

我有一个像这样的简单组件

import { Component } from 'react'

export default class SearchList extends Component(){
    constructor(props){
        super(props);
    }
    render(){
        const { placeholder } = this.props;
        return(
            <div className="searchList">
                <input type="text" placeholder={placeholder}/>
                <button>Search</button>
            </div>
        )
    }
}

我在某处执行<SearchList placeholder="Search Area" />

为什么会出现无法设置未定义道具属性的错误?

当您编写扩展React.Component组件时,在React.Component之后不需要多余的()

用这个

export default class SearchList extends Component{
    constructor(props){
        super(props);
    }
    render(){
        const { placeholder } = this.props;
        return(
            <div className="searchList">
                <input type="text" placeholder={placeholder}/>
                <button>Search</button>
            </div>
        )
    }
}

暂无
暂无

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

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