Javascript 无法读取属性';查拉特';在风暴中未定义的
谁能帮助我理解我在这里做错了什么。我是JavaScript编程新手。我遇到以下错误: 无法读取未定义的属性“charAt” 守则:Javascript 无法读取属性';查拉特';在风暴中未定义的,javascript,Javascript,谁能帮助我理解我在这里做错了什么。我是JavaScript编程新手。我遇到以下错误: 无法读取未定义的属性“charAt” 守则: import React from 'react'; export default class Page extends React.Component { render() { return ( <div className="container"> <h2>
import React from 'react';
export default class Page extends React.Component {
render() {
return (
<div className="container">
<h2>{this.props.currentPage.charAt(0).toUpperCase() + this.props.currentPage.slice(1)}</h2>
<p>This is the {this.props.currentPage} page</p>
{/* example of using chain methods inside the curly brackets to transform and concat string */}
</div>
)
}
}
从“React”导入React;
导出默认类页面扩展React.Component{
render(){
返回(
{this.props.currentPage.charAt(0.toUpperCase()+this.props.currentPage.slice(1)}
这是{This.props.currentPage}页面
{/*在花括号内使用chain方法转换和concat字符串的示例*/}
)
}
}
您可以保护代码:
((this.props.currentPage || "").charAt(0) || "").toUpperCase()
该错误意味着
this.props.currentPage
是未定义的
您可能希望使用this.state
而不是this.props
。还包括一个构造函数和一个this.setState()
方法。