英文:
Why am I getting a "Objects are not valid as a React child" error? I'm not passing an object
问题
我正在学习,并遇到了这个错误。我的意图是让payrate
充当父组件,而Timer
和TodaysPay
充当子组件。当按钮被点击时,Timer
设置一个计时器,而./today
显示总工资。总工资是在父组件./payrate
中使用timer
和用户输入作为属性来计算的。我之所以使用today
作为子组件,是因为将来可能会为其添加功能。
我正在使用React JS和Vite。没有后端,我打算稍后添加localStorage
。
我试图在父组件中计算总工资,并将其用作属性在子组件中显示。但我无法摆脱"对象不可用作React子元素"的错误。你们中是否有人能看出我为什么会遇到这个错误,以及如何解决它?
import React, { useState } from 'react';
import Timer from './timer';
import TodaysPay from './today';
const PayRate = (props) => {
// 处理表单
const [inputRate, setInputRate] = useState('');
const handleRate = (event) => {
setInputRate(event.target.value);
}
const handleSubmit = (event) => {
event.preventDefault();
console.log(`每小时费率是 ${inputRate}`);
}
// 这将小时工资计算为秒数
const { seconds } = props;
const grossPay = (inputRate / 3600) * seconds;
// 结束工资计算
return (
<div>
<h1>
{inputRate}
</h1>
<TodaysPay grossPay={grossPay} />
<Timer />
<form onSubmit={handleSubmit}>
<input placeholder="每小时费率" type="number" value={inputRate} onChange={handleRate} />
<button type="submit">提交</button>
</form>
</div>
);
}
export default PayRate;
import React from 'react'
import PayRate from './payrate';
// 显示在./payrate.jsx中计算的总工资
export default function TodaysPay(props) {
const grossPay = props.grossPay;
return (
<div>
{grossPay}
</div>
)
}
我一直在使用ChatGPT,并且它将我的代码片段带到了现在,但我仍然遇到错误。我还使用了Google,但找不到与我的代码相关的答案。我之前在TodaysPay
的属性中有更多的属性,但后来意识到我不需要它们,所以我将它们删除了,但没有帮助。
英文:
I'm learning, and having an issue with this error. My intention is for payrate to act as the parent, with to child components. Timer sets a timer when the button is clicked, and ./today displays the gross pay. The gross pay is calculated in the parent ./payrate component, using timer and user input as props.The reason I'm using today as a child component, is because I may add functionality to it in the future.
I am using React JS and Vite. No backend, I intend to add localStorage to this later on.
I am trying to calculate the gross pay in the parent component, and use it as props to display in the child component. But i can't get rid of this "Objects are not valid as a React child" error. Can any of you see why I am getting this error, and how I can fix this?
import React, { useState } from 'react';
import Timer from './timer';
import TodaysPay from './today';
const PayRate = (props) => {
// handles the form
const [inputRate, setInputRate] = useState('');
const handleRate = (event) => {
setInputRate(event.target.value);
}
const handleSubmit = (event) => {
event.preventDefault();
console.log(`hourly rate is ${inputRate}`);
}
// this calculates the hourly pay into seconds
const {seconds} = props;
const grossPay = (inputRate / 3600) * seconds;
// end pay calculation
return (
<div>
<h1>
{inputRate}
</h1>
<TodaysPay grossPay={grossPay} />
<Timer />
<form onSubmit={handleSubmit}>
<input placeholder="Hourly Rate" type="number" value={inputRate} onChange={handleRate} />
<button type="submit">Submit</button>
</form>
</div>
);
}
export default PayRate;
import React from 'react'
import PayRate from './payrate';
// displays the grosspay, calculated in ./payrate.jsx
export default function TodaysPay(props) {
const grossPay = props;
return (
<div>
{grossPay}
</div>
)
}
I have been using chatGPT and it got me to where my code snippets are now, but I still have the error. I have also been using google, but I can't find answer that seem relevant to my code. I did have more props in the TodaysPay attrubutes, but realized I didn't need them, so i removed them, but it didn't help.
答案1
得分: 1
props
是函数组件(TodaysPay)中的一个对象
这一行:const grossPay = props;
应该改为:const { grossPay } = props;
或 const grossPay = props.grossPay;
或
export default function TodaysPay({ grossPay }) {
return (
<div>
{grossPay}
</div>
)
}
英文:
props
is an object in function components (TodaysPay)
this line: const grossPay = props;
should be: const { grossPay } = props;
or const grossPay = props.grossPay;
or
export default function TodaysPay({ grossPay }) {
return (
<div>
{grossPay}
</div>
)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论