TypeError: Cannot read property startsWith of undefined
07:59 06 May 2020
import React from 'react';

import AttributeDescription from './AttributeDescription';

class CompEntry extends React.Component{

render(){

    let description;

    if(this.props.description.startsWith("_")){
        description= this.props.description.slice(1, this.props.description.length);
    }
    if(this.props.description.startsWith("__")){
        description = this.props.description.slice(2, this.props.description.length);
    }

    return(
        
); }; } export default CompEntry;

The mentioned error happened if I do the stuffs before the return. However, if i dont do anything before the return and just pass this props.description into the description prop of the tag, everything works fine, a defined props is passed into the tag. It seems like if the value of this.props.description does not exist if i try to access its property. Anyone knows why?

This is how I use the CompEntry component above:

import React from 'react';
import CompEntry from './CompEntry';
import CompHeading from './CompHeading';

class CompTable extends React.Component{
constructor(props){
    super(props);
    this.state = {
        products: [],
        attributes: [],
        attDesc: [],
    };
    this.getEntries = this.getEntries.bind(this);
}

getEntries = async () => {
    const response = await fetch('/api/hello/data');
    const body = response.json();
    return body;
};

componentDidMount(){
    this.getEntries()
        .then((resolve) => this.setState({
            products: resolve.products, 
            attributes: resolve.attributes, 
            attDesc: resolve.attributesDescription}))
        .catch(err=>console.log(err));
};


render(){
    console.log(this.state.products);
    let highlightEntry= true;
    let compEntries = this.state.attributes.map( (item, index) =>{
       highlightEntry = !highlightEntry; 

       return(
                       
       );
    });

    return(
        
{compEntries}
); } } export default CompTable;
node.js reactjs