0

I get TypeError: Cannot read property 'props' of undefined. I need to my Li component to receive props. I'm using styled-component by the way.

const Li = styled.li`
  border-top: 0 none;
`;

class Popular extends Component {
    render() {
        var languages = ['All', 'Javascript', 'Java', 'Python'];
        return (
            <ul className='list-group'>
                {languages.map(function(lang){
                    return (
                        <Li className={`list-group-item p-2 ${this.props.className}`}>
                            {lang}
                        </Li>
                    )
                })}
            </ul>
        );
    }
}

export default Popular;
letthefireflieslive
  • 11,493
  • 11
  • 37
  • 61

1 Answers1

3

You need to bind the callback of languages.map to the popular class. You can either use arrow function like this

{languages.map((lang) => {

or just pass this as the second argument of map function like this

{languages.map(function(lang){
       ...
}, this)}

or you can bind it using bind function like this

{languages.map((function(lang){
       ...
}).bind(this))}
Prakash Sharma
  • 15,542
  • 6
  • 30
  • 37