Why am I getting a "Objects are not valid as a React child" error? I'm not passing an object

huangapple go评论108阅读模式
英文:

Why am I getting a "Objects are not valid as a React child" error? I'm not passing an object

问题

我正在学习,并遇到了这个错误。我的意图是让payrate充当父组件,而TimerTodaysPay充当子组件。当按钮被点击时,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 &#39;react&#39;;
import Timer from &#39;./timer&#39;;
import TodaysPay from &#39;./today&#39;;

const PayRate = (props) =&gt; { 
   // handles the form         
  const [inputRate, setInputRate] = useState(&#39;&#39;);

  const handleRate = (event) =&gt; {
      setInputRate(event.target.value);
    }

  const handleSubmit = (event) =&gt; {
    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 (
    &lt;div&gt;
      &lt;h1&gt;
        {inputRate}
      &lt;/h1&gt;
      &lt;TodaysPay grossPay={grossPay} /&gt;
      &lt;Timer /&gt;
      &lt;form onSubmit={handleSubmit}&gt;
        &lt;input placeholder=&quot;Hourly Rate&quot; type=&quot;number&quot; value={inputRate} onChange={handleRate} /&gt;
        &lt;button type=&quot;submit&quot;&gt;Submit&lt;/button&gt;
      &lt;/form&gt;
    &lt;/div&gt;
  );
}
  export default PayRate;

import React from &#39;react&#39;
import PayRate from &#39;./payrate&#39;;

// displays the grosspay, calculated in ./payrate.jsx
export default function TodaysPay(props) {
  const grossPay = props;
  return (
    &lt;div&gt;
      {grossPay}
    &lt;/div&gt;
  )
}

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 (
      &lt;div&gt;
        {grossPay}
      &lt;/div&gt;
    )
}

huangapple
  • 本文由 发表于 2023年2月8日 19:32:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定