3

I am trying to shorten a string in a react component I got from an API call in react but I get TypeError: Cannot read property 'substring' of undefined.

here is the snippet:

shorten(){
    let res = this.props.details.substring(100, 0);
    return res;
}

I am using the function in a render return function as:

    render(){
        return (
            <div>
                <p className="text-muted font1-1">{this.shorten()}</p>
            </div>
        );
    }

Error code:

TypeError: Cannot read property 'substring' of undefined
3
  • 1
    Your details prop has no value. Commented May 14, 2020 at 10:51
  • it has a value, I console logged to confirm. Commented May 14, 2020 at 10:53
  • 1
    You got the value from an API. So your value, in the beginning, is undefined. After when the API comes back, you got the value, and you saw this result in console, but the first time when react renders your component the details prop has no value. Commented May 14, 2020 at 10:55

5 Answers 5

4

Get rid of the shorten method and try this

render(){
    const shorten = this.props.details ? this.props.details.substring(0, 100) : '';
        return (
            <div>
                <p className="text-muted font1-1">{shorten}</p>
            </div>
        );
    }
Sign up to request clarification or add additional context in comments.

Comments

3

Since the error happens just before the API call is made, use optional chaining (?.) to check whether the string to be truncated is available yet.

let res = this.props.details?.substring(100, 0);

Comments

2

Add a null check before use substring, like this: let res = this.props.details && this.props.details.substring(100,0);

1 Comment

Thank you, now I know I have to check for null because of how React renders.
1

did you tried to console.log the value of shorten? Do you get undefined in the console too?

if so, is it possible that you are not waiting for the API value.

Are you familiar with the concept of promise ? My hypothesis is that you get undefined because you are trying to see the result without have some async/await in place (or some other way to wait for the promise to be resolved), which means that you are trying to read the value before it actually exist, that is why you get the undefined

2 Comments

the value from the console.log of the shorten value works as expected. Apparently I have to check for null value first. Thank you.
Yes that too, because of the React Lifecycle The important thing is that you got the solution, have a nice day =)
1

If you're sure that the prop is pass probably there's some state of the parent that causes this.props.details to be undefined.

Ther are 3 solutions.

  1. When you're passing the prop you can add a check
<YourComponent details={this.state.whatever || '' } />
  1. you can check it inside the component
let res = this.props.details && this.props.details.substring(100, 0);
  1. Don't render the component until all required props are passed and they're defined.

1 Comment

Thanks, I followed a similar solution. Problem solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.