最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - react cannot set property of props of undefined - Stack Overflow

programmeradmin1浏览0评论

I have a simple ponent like this

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>
        )
    }
}

The somewhere I do <SearchList placeholder="Search Area" />

Why I got error of cannot set property of props of undefined?

I have a simple ponent like this

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>
        )
    }
}

The somewhere I do <SearchList placeholder="Search Area" />

Why I got error of cannot set property of props of undefined?

Share Improve this question asked Mar 28, 2017 at 6:07 Alex YongAlex Yong 7,6558 gold badges27 silver badges42 bronze badges 1
  • 2 remove () from export default class SearchList extends Component() – Ved Commented Mar 28, 2017 at 6:11
Add a ment  | 

1 Answer 1

Reset to default 6

When you write a react ponent extending React.Component you don't need the extra () after React.Component

Use this

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>
        )
    }
}
发布评论

评论列表(0)

  1. 暂无评论