2
votes

I'm following a tutorial online for React.js on controlled inputs and I am repeatedly getting the error

TypeError: Cannot read property 'map' of undefined

import CallRow from "./CallRow";
import React from "react";

class SearchPage extends React.Component {
  constructor() {
    super();
    this.state = {
      search: "Level Up"
    };
  }

  updateSearch(event) {
    this.setState({ search: event.target.value.substr(0, 20) });
  }

  render() {
    return (
      <div>
        <ul>
          {this.props.calls.map(call => (
            <CallRow call={call} key={call.id} />
          ))}
        </ul>
        <input
          type="text"
          value={this.state.search}
          onChange={this.updateSearch.bind(this)}
        />
      </div>
    );
  }
}

export default SearchPage;
3
I think it would help if you can also share how you are using the component SearchPage. That code you share should help you and other people answer the following questions: 1) Did you provide calls as props to the component; 2) If you did, did you check that it was not undefined? - Imran Ariffin

3 Answers

2
votes

It seems that the call prop undefined.

Before map the prop value, check if it is undefined or not.

Please refer below code:

<ul>
    {(this.props.calls || []).map(call => (
        <CallRow call={call} key={call.id} />
    ))}
</ul>
1
votes

You should add props when calling super, also you not showing the whole application logic, are props.calls are even defined?

class SearchPage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      search: "Level Up"
    };
  }
...
0
votes

1 check if you add the props into the component SearchPage as

<SearchPage calls ={arry}/>

and cleck if this array or not 2add in

constructor(props) {
    super(props);

after the make destructer for props by this const {calls}=this.props; then check the value of the calls if it pass as props by used console.log(calls)