无法在React中的组件之间传递数据

发布于 2025-01-22 00:04:27 字数 2621 浏览 0 评论 0原文

我正在制作一个简单的计算器应用程序,其中我想在其中显示显示组件中按钮组件的值,但是我没有得到如何执行此操作的

代码:

计算器组件的代码:< /strong>

import React from "react";
import "./Calculator.css";
import Button from "./Button";
import { useState } from "react";
import Display from "./Display";

const Calculator = () => {
  const [Data, setData] = useState("");
  return (
    <>
      <center>
        <h1 className="title">Calculator</h1>
        <br />
        <Display data={Data} />
        <div className="btncontainer">
          <Button name="+" onClick={() => setData("+")}></Button>
          <Button name="-" onClick={() => setData("-")}></Button>
          <Button name="/" onClick={() => setData("/")}></Button>
          <Button name="X" onClick={() => setData("X")}></Button>
          <Button name="1" onClick={() => setData("1")}></Button>
          <Button name="2" onClick={() => setData("2")}></Button>
          <Button name="3" onClick={() => setData("3")}></Button>
          <Button name="4" onClick={() => setData("4")}></Button>
          <Button name="5" onClick={() => setData("5")}></Button>
          <Button name="6" onClick={() => setData("6")}></Button>
          <Button name="7" onClick={() => setData("7")}></Button>
          <Button name="8" onClick={() => setData("8")}></Button>
          <Button name="9" onClick={() => setData("9")}></Button>
          <Button name="0" onClick={() => setData("0")}></Button>
          <Button name="=" onClick={() => setData("=")}></Button>
        </div>
      </center>
    </>
  );
};

export default Calculator;
 

显示组件的代码:

import React from "react";

const Display = (props) => {
  console.log("Props in display : ", props);
  const handleOnChange = (e) => {
    props.data = e.target.value;
  };
  return (
    <input type="text" value={props.data} onChange={handleOnChange} readOnly />
  );
};

export default Display;
 

按钮组件的代码:

import React from "react";
import "./Button.css";
const Button = (props) => {
  return (
    <input
      type="button"
      className="btn"
      value={props.name}
      onClick={props.onClick(props.name)}
    ></input>
  );
};

export default Button;
 

当我刚开始学习React时,请忽略我的愚蠢错误

I am making a simple calculator app with react in which I want to display the value of the button component in the Display component, but I'm not getting how to do this

Here are the Codes :

Code Of Calculator Component:

import React from "react";
import "./Calculator.css";
import Button from "./Button";
import { useState } from "react";
import Display from "./Display";

const Calculator = () => {
  const [Data, setData] = useState("");
  return (
    <>
      <center>
        <h1 className="title">Calculator</h1>
        <br />
        <Display data={Data} />
        <div className="btncontainer">
          <Button name="+" onClick={() => setData("+")}></Button>
          <Button name="-" onClick={() => setData("-")}></Button>
          <Button name="/" onClick={() => setData("/")}></Button>
          <Button name="X" onClick={() => setData("X")}></Button>
          <Button name="1" onClick={() => setData("1")}></Button>
          <Button name="2" onClick={() => setData("2")}></Button>
          <Button name="3" onClick={() => setData("3")}></Button>
          <Button name="4" onClick={() => setData("4")}></Button>
          <Button name="5" onClick={() => setData("5")}></Button>
          <Button name="6" onClick={() => setData("6")}></Button>
          <Button name="7" onClick={() => setData("7")}></Button>
          <Button name="8" onClick={() => setData("8")}></Button>
          <Button name="9" onClick={() => setData("9")}></Button>
          <Button name="0" onClick={() => setData("0")}></Button>
          <Button name="=" onClick={() => setData("=")}></Button>
        </div>
      </center>
    </>
  );
};

export default Calculator;
 

Code Of Display Component:

import React from "react";

const Display = (props) => {
  console.log("Props in display : ", props);
  const handleOnChange = (e) => {
    props.data = e.target.value;
  };
  return (
    <input type="text" value={props.data} onChange={handleOnChange} readOnly />
  );
};

export default Display;
 

Code Of Button Component:

import React from "react";
import "./Button.css";
const Button = (props) => {
  return (
    <input
      type="button"
      className="btn"
      value={props.name}
      onClick={props.onClick(props.name)}
    ></input>
  );
};

export default Button;
 

As I've just started learning React, please ignore my silly mistakes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2025-01-29 00:04:27

在按钮组件中,如果要将参数传递给该函数,则要将OnClick设置为箭头函数。

onclick = {()=&gt; props.onclick(props.name)}

在显示组件中,您应该传递setData函数,并将其用于OnChange。

onChange = {(e)=&gt; setData(e.target.value)}

如果要单击所有按钮的字符串,则可以更改按钮onclick以添加到当前数据值而不是替换。
例如,对于按钮5:

onclick = {()=&gt; setData(data+'5'}

In the Button component, if you want to pass an argument to the function, you want to set the onClick to an arrow function.

onClick={()=>props.onClick(props.name)}

In the Display component, you should pass in the setData function and use that for the onChange.

onChange={(e)=>setData(e.target.value)}

If you want a string of all the buttons clicked, you can change the button onClick to add to the current value of data instead of replacing it.
For example, for button 5:

onClick={()=>setData(data+'5'}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文