[英]How to pass integer value from parent prop to child prop in React
Basically I am working on a dice game where I have a class for Number of sides and another class for Number of dices.基本上我正在开发一个骰子游戏,我有一个关于边数的类和另一个关于骰子数的类。 I want to get values from both classes and pass them to a child class where I will multiply both together to get
maxScore
.我想从两个类中获取值并将它们传递给一个子类,在那里我将两者相乘以获得
maxScore
。 All classes are in same file所有类都在同一个文件中
However, I get NaN
as a result.但是,结果我得到了
NaN
。 I feel like I am not passing values correctly.我觉得我没有正确传递值。 All classes are in same file.
所有类都在同一个文件中。 I am new to React so any help would be appreciated.
我是 React 的新手,所以任何帮助将不胜感激。
In both cases you are missing out one of the props
values.在这两种情况下,您都错过了
props
值之一。 So the component tries to multiply undefined
with a number and that's why you are getting NaN
.因此该组件尝试将
undefined
与一个数字相乘,这就是您得到NaN
的原因。
Think about the following:考虑以下问题:
console.log(6 * undefined); console.log(undefined * 2);
As a solution you need to provide both values for <Child>
component.作为解决方案,您需要为
<Child>
组件提供两个值。 Try the following:请尝试以下操作:
<Child noOfSides={this.state.mystate} noOfDice={1} />
{/* or */}
<Child noOfDice={this.state.mystate} noOfSides={1}/>
I hope this explains!我希望这能解释!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.