javascript - Uncaught TypeError: Cannot read property 'filter' of undefined -
import react, {purecomponent} 'react'; import {textinput} '../../shared'; import {array} 'prop-types'; import { employeetable } '../employeetable/employeetable'; import './headersearch.scss'; export class headersearch extends purecomponent { static proptypes = { employees: array } constructor (props) { super(props); this.state = { searchvalue: null }; } _updatesearchvalue (value) { this.setstate({ searchvalue: value }); } render () { const employees = this.props.employees; let filteredemployees = employees.filter( (employee) => { return employee.name.indexof(this.state.searchvalue) !== -1; } ); return ( <div classname='header_search'> <ul> {filteredemployees.map((employee) => { return <employeetable employees={employee} key={employee.id} />; })} </ul> <textinput label='search people' value={this.state.searchvalue} onchange={(e) => this._updatesearchvalue(e.target.value)} /> </div> ); } } export default headersearch;
i'm newbie @ reactjs i'm getting stuck on problem. realize question has been asked , looked through them still couldn't find solution why i'm getting error. want filter array employees
according searchvalue
, display update employee table
.
just add default value headersearch
import react, {purecomponent} 'react'; import {textinput} '../../shared'; import {array} 'prop-types'; import { employeetable } '../employeetable/employeetable'; import './headersearch.scss'; export class headersearch extends purecomponent { static defaultprops = { // <-- default props employees: [] // undefined gets converted array,render won't trigger error } static proptypes = { employees: array } constructor (props) { super(props); this.state = { searchvalue: null }; } _updatesearchvalue (value) { this.setstate({ searchvalue: value }); } render () { // omitted } } export default headersearch;
error triggers when employees
prop not provided, or null or undefined, when provide default value of empty array, array.filter
won't throw error, because default value of employees
instance of array
Comments
Post a Comment