admin管理员组文章数量:1357672
import React, {PureComponent} from 'react';
import {TextInput} from '../../shared';
import {array} from 'prop-types';
import { EmployeeTable } from '../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 for people'
value={this.state.searchValue}
onChange={(e) => this._updateSearchValue(e.target.value)}
/>
</div>
);
}
}
export default HeaderSearch;
I'm a newbie at ReactJS so I'm getting stuck on this problem. I realize this question has been asked and I looked through them all but still couldn't find a solution as to why I'm getting this error. I want to filter the array employees
according to the searchValue
and display the update Employee Table
.
import React, {PureComponent} from 'react';
import {TextInput} from '../../shared';
import {array} from 'prop-types';
import { EmployeeTable } from '../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 for people'
value={this.state.searchValue}
onChange={(e) => this._updateSearchValue(e.target.value)}
/>
</div>
);
}
}
export default HeaderSearch;
I'm a newbie at ReactJS so I'm getting stuck on this problem. I realize this question has been asked and I looked through them all but still couldn't find a solution as to why I'm getting this error. I want to filter the array employees
according to the searchValue
and display the update Employee Table
.
-
Well like all other questions with this error, it means you are trying to use something on a variable that is undefined. Meaning a variable doesn't hold what you think it does. Make sure
employees
is actually an array (eg console.log it) – Patrick Evans Commented Aug 14, 2017 at 20:05
2 Answers
Reset to default 2Just add default value for HeaderSearch
import React, {PureComponent} from 'react';
import {TextInput} from '../../shared';
import {array} from 'prop-types';
import { EmployeeTable } from '../employeeTable/EmployeeTable';
import './HeaderSearch.scss';
export class HeaderSearch extends PureComponent {
static defaultProps = { // <-- DEFAULT PROPS
employees: [] // undefined gets converted to 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 is not provided, or null or undefined, when you provide default value of empty Array, the Array.filter
won't throw error, because default value of employees
is an instance of Array
As the error message tells you, problem is that your constant employees
is undefined. The problem will be in a place where you are rendering HeaderSearch
ponent. Most likely you are not passing it the props you think you are. Try to render<HeaderSearch employees={[]} />
. Does it work? What does console.log(this.props)
show you? Can you see key employees
there?
If you need more assistance please post the code where you are actually rendering HeaderSearch
ponent.
本文标签: javascriptUncaught TypeError Cannot read property 39filter39 of undefinedStack Overflow
版权声明:本文标题:javascript - Uncaught TypeError: Cannot read property 'filter' of undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744079933a2587441.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论