reactjs - 反应:无法读取未定义的属性“值”
问题描述
我正在尝试制作一个 React 计算器。我正在为初始测试构建组件,但我似乎无法编译它。我认为我没有将状态作为道具正确地传递给 Display 组件,但我真的不知道我做错了什么。
import React, { Component } from "react";
import "./App.css";
class Calculator extends Component {
constructor(props) {
super(props);
this.state = { value: "[]" };
}
render() {
return (
<div id="container">
<Button value={"1"} type="input" />
<Button value={"2"} type="input" />
<Button value={"3"} type="input" />
<Button value={"4"} type="input" />
<Button value={"5"} type="input" />
<Button value={"6"} type="input" />
<Button value={"7"} type="input" />
<Button value={"8"} type="input" />
<Button value={"9"} type="input" />
<Button value={"."} type="input" />
<Button value={"="} type="input" />
<Button value={"Clear"} type="input" />
<Button value={"add"} type="input" />
<Button value={"substract"} type="input" />
<Button value={"multiply"} type="input" />
<Button value={"divide"} type="input" />
<Display value={this.state.value} />
</div>
);
}
}
const Button = props => {
return <input type="Button" value={props.value} />;
};
const Display = props => {
return (
<div>
<p>{props.state.value}</p>
</div>
);
};
解决方案
value
提供给您的组件
的道具Display
将被调用value
,而不是state.value
:
const Display = props => {
return (
<div>
<p>{props.value}</p>
</div>
);
};
推荐阅读
- javascript - 如何在子目录中包含nodejs模块
- java - JAXB 解组中的 NULL 值
- asp.net - 如何结合现有的 ASP.NET 站点通过 IIS 重定向 Wordpress 博客
- javascript - 如何使用 swup.js 进行链接页面转换
- html - 如何为 span 元素设置不同的 margin-top?
- java - 需要帮助在主类中添加方法
- html - html 外的空白区域
- python - 修复此代码'不能在内部使用几何管理器网格。已经有由包管理的奴隶'
- javascript - 正方形的圆形旋转。Phaser3
- ubuntu - 创建 Flutter 项目时出现权限错误