Cannot read property of undefined (read 'split') TypeError: Cannot read property of undefined (read 'split')
P粉094351878
P粉094351878 2023-07-28 20:05:15
0
1
444

I'm trying to get the release year from the release_date (as props) passed to a React function component, but I'm getting a string undefined error.

const Banner = ({ backdrop_path, poster_path ,title,release_date}) => { return ( 
{title} ({release_date.split("-")[0]})
); }; export default Banner;

When I use release_date directly, it renders fine, no problem. But when I apply any method on string it returns undefined. (release_date is a string, such as "2023-07-18")

P粉094351878
P粉094351878

reply all (1)
P粉311423594

I recommend adding a condition to ensure I don't have empty brackets (e.g. if your data get returns empty).

{title} {release_date && ` (${release_date.split("-")[0]})`}
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!