TypeError: Cannot read property 'toUpperCase' of undefined (React)

时间:2018-06-04 17:21:14

标签: javascript reactjs

I want to get the name of the page from props match, here's the attempt below on first boot

The console does not display the call to this.props.match.path

import React, {Component} from 'react'
import Post from './Post'

export default class Posts extends Component {
    state = {
      namePage: ''
   };
   componentWillMount() {
    this.setState({
        namePage: this.props.match.path.slice(1)[0].toUpperCase() + this.props.match.path.slice(2)
    })
  render(){
    return (
             <div>
                   <h2>{this.state.namePage}</h2>
             </div>

    )
}

}
введите сюда описание изображения that's what I get when starting the reaction, help me understand what I'm doing wrong

1 个答案:

答案 0 :(得分:0)

你能澄清一下为什么你只选择路径的第一个字符,然后只获取第一个数组中的第一个字符,并将其与下一个第一个字符连接起来吗?您对输出的期望是什么样的结果?

尝试从切片中删除[0]并查看是否返回您要查找的内容。