react cannot set property of props of undefined

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

I have a simple component 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?

回答1:

When you write a react component 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>         )     } } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!