【发布时间】:2019-11-08 02:30:03
【问题描述】:
我正在尝试将值从一个组件 - Counters.jsx 传递到 Counter.jsx。当我转到开发人员控制台并执行日志时,我可以从以下位置获取数据:this.props.value 但是当我尝试将其设置为状态时,我收到以下错误:TypeError: Cannot read property 'value'的未定义。
// This is the component: Counters
import React, { Component } from "react";
import Counter from "./counter";
class Counters extends Component {
state = {
counters: [
{ id: 1, value: 4 },
{ id: 2, value: 0 },
{ id: 3, value: 0 },
{ id: 4, value: 0 }
]
};
render() {
return (
<div>
{this.state.counters.map(counter => (
<Counter key={counter.id} value={counter.value} selected={true} />
))}
</div>
);
}
}
// This is the component: Counter
import React, { Component } from "react";
class Counter extends Component {
state = {
count: this.props.value
};
当我将状态 - Count 设置为 this.props.value 时,问题出在 Counter 类中。但是,如果我执行 console.log(this.props.value),则可以访问此值。
我收到此错误:未捕获的类型错误:无法读取未定义的属性“值”
有谁知道我是否遗漏了什么,为什么我可以使用 console.log 访问它但无法将状态计数设置为 this.props.value?
【问题讨论】:
标签: reactjs components state prop