reactjs - this.props.chatVisible 是“未定义”
问题描述
我想在 ChatTitleButton 类中使用 chatVisible 道具。但是当我 console.log(this.props.chatVisible); 我会变得不确定。而且我不知道为什么我一直不确定。因为它必须用作布尔值,所以 React 可以根据该变量呈现要呈现的 html。我希望有人能帮助我
import ChatTitleButton from "./ChatTitleButton";
class Chat extends React.Component {
constructor(props) {
super(props);
this.state = {
chatVisible: true,
};
}
render() {
return (
<div classname = "chat-widget" >
<ChatTitleButton / >
</div>
)
}
}
import React from 'react';
export default class ChatTitleButton extends React.Component {
render() {
if (this.props.chatVisible === true) {
return ( <
div className = "chat-title-close" >
<
button onClick = {
this.closeChat
}
className = "btn chat-close" > Chat beindigen < /button> <
/div>
)
} else {
return ( <
div className = "chat-title-close" >
<
button onClick = {
this.openChat
}
className = "btn chat-close" > Chat openen < /button> <
/div>
)
}
}
}
解决方案
您没有将道具传递给组件。在您的渲染语句中,而不是<ChatTitleButton />
使用<ChatTitleButton chatVisible={this.state.chatVisible}/>
.
推荐阅读
- python - 使用 LSTM 网络和 Keras 进行文本分类,准确率为 0.0%
- php - 如何更改表单生成器 DateType 字段(Symfony 4)的格式?
- shell - 如何使用 touch t- 功能修复“触摸:超出范围或非法时间规范”
- php - ajax 发布后的 Phpmyadmin 奇怪的字符
- javascript - Promise.all.then 并非所有代码路径都返回值
- c# - 从通用抽象类派生类和转换错误?
- php - mysql_fetch_array 只返回一行
- xml - XSLT 转换不适用于 Chrome 71
- php - 我应该使用 $_SESSION 还是 isset($_SESSION)
- parsing - 第一个位置的 ANTLR 无关输入不会构建树