reactjs - TypeError:无法读取 null 的属性“withFooter”
问题描述
我正在尝试在反应打字稿中创建一个布局元素。我试图有一个布尔道具来确定是否渲染页脚。不幸的是,我得到了一个无法读取未定义问题的属性。我还想为 prop 类型设置一个默认的 true 值。我是新来的反应,但仍在努力学习细节。
import React, { Component } from 'react'
import Scrollbar from 'react-smooth-scrollbar'
import Header from '../Header/index'
import Footer from '../Footer/index'
type Props = {
withFooter: boolean
}
class Layout extends Component<{}, Props> {
render() {
let footer
if (this.state.withFooter) {
footer = <Footer />
} else {
footer = null
}
return (
<div>
<Scrollbar>
<Header />
{this.props.children}
{footer}
</Scrollbar>
</div>
);
}
}
export default Layout
解决方案
你能试试这个
type Props = {
withFooter: boolean
}
class Layout extends Component<Props, {}> {
public static defaultProps = {
withFooter: true,
};
render() {
let footer
if (this.props.withFooter) {
footer = <Footer />
} else {
footer = null
}
return (
<div>
<Scrollbar>
<Header />
{this.props.children}
{footer}
</Scrollbar>
</div>
);
}
}
推荐阅读
- c# - 在 MVC 中使用双重值
- regex - 如何按照自定义模式从变量获取所有字符串实例到bash中的数组?
- r - 获取 c++ 异常尝试使用 rvest 和 V8 抓取 javascript 呈现的网页
- css - 我如何内联显示 React 组件
- python - 使用 Beautiful Soup 在嵌套标签中查找价值
- visual-studio - 如何解决 nuget 包错误?
- php - 在 PHP 中循环并存储关联数组中的键值
- sas - 如何使用 palote 字符作为分隔符在 sas 中创建 .txt 文件?
- vue.js - 如何使用 VueBlobJsonCsv 从 API 提取数据并转换为 CSV
- c# - 如何正确地将 C# 中的双精度数组发送到 C dll?