我希望统计组件只显示在点击后,我已经创建的3个按钮的任何一个.当我试图将统计组件移动到历史组件(在第二个返回中,下面的按钮按下历史:...)时,我现在面临着变量未定义的问题(好、中性、坏、ALL2都没有定义).如何使用我在一个组件中定义的变量,以便它可以在其他组件中使用?这是我的代码,

App.js

import { useState } from 'react'

const History = (props) => {
  if (props.allClick.length === 0) {
    return (
      <div>
        the app is used by pressing the buttons
      </div>
    )
  }

  return (
    <div>
      button press history: {props.allClick.join(' ')}  
        
    </div>
  )
}

const Button = (props) => {
  return (
    <div>
      <button onClick={props.handleClick}> {props.text} </button>
    </div>
  )
}

const Statistic = (props) => {
  return (
    <div>
      <p>{props.title} {props.type} </p>
    </div>
  )
}

const App = () => {
  // save clicks of each button to its own state
  const [good, setGood] = useState(0)
  const [neutral, setNeutral] = useState(0)
  const [bad, setBad] = useState(0)
  const [allClick, setClick] = useState([])

  const inlineButton = {
    display: 'inline-flex',
    gap: '10px'
  }

  const goodClick = () => {
    setClick(allClick.concat('G'))
    setGood(good + 1)
  }

  const neutralClick = () => {
    setClick(allClick.concat('N'))
    setNeutral(neutral + 1)
  }

  const badClick = () => {
    setClick(allClick.concat('B'))
    setBad(bad + 1)
  }

  const all2 = good+neutral+bad

  return (
    <div>
      <h1> give feedback </h1>
      <div style={inlineButton}>
        <Button handleClick={goodClick} text="good"/>
        <Button handleClick={neutralClick} text="neutral"/>
        <Button handleClick={badClick} text="bad" />
      </div>

      <h1> statistics </h1>

      <History allClick={allClick} />

      <Statistic title="good" type={good}/>
      <Statistic title="neutral" type={neutral}/>
      <Statistic title="bad" type={bad}/>
      <Statistic title="all" type={all2}/>
      <Statistic title="positive" type={good/all2 * 100} />  
    
    </div>
  )
}

export default App 

Index.js

import React from 'react'
import ReactDOM from 'react-dom/client'

import App from './App'

ReactDOM.createRoot(document.getElementById('root')).render(<App />)

帮助传递变量.可能是新的代码块.

推荐答案

在App.js中,

<History allClick={allClick} good={good} neutral={neutral} bad={bad} />

在历史记录组件中,

const History = (props) => {
  if (props.allClick.length === 0) {
    return <div>the app is used by pressing the buttons</div>;
  }

  const all2 = props.good + props.neutral + props.bad;

  return (
    <>
      <div>button press history: {props.allClick.join(" ")}</div>
      <Statistic title="good" type={props.good} />
      <Statistic title="neutral" type={props.neutral} />
      <Statistic title="bad" type={props.bad} />
      <Statistic title="all" type={all2} />
      <Statistic title="positive" type={(props.good / all2) * 100} />
    </>
  );
};

Laravel相关问答推荐

在GitHub操作中缺少PHPStan和PHPMD输出,但在本地存在

LaravelEloquent 的模型如何将值从控制器内部的函数传递到Render()

Laravel 10 - 没有 $append 属性的Eloquent 的热切加载

分页计数(*)查询问题

运行Docker时,安装PHP8.1时返回错误

(1/1) ErrorException ReflectionFunction::__construct() ex

Eloquent/Laravel:是否有 object->load('relation') 的对应物,例如.object->unload('relation')?

Laravel/Eloquent:致命错误:在非对象上调用成员函数 connection()

从 Laravel Session 数组中删除项目

如何使用 Laravel 模型访问数据库视图?

Laravel 5 - 在控制器文件中为多个路由定义中间件

laravel 4 - 如何限制(采取和跳过)Eloquent的 ORM?

413请求实体在laravel homestead for windows中太大的nginx服务器

如何在 Laravel 5.1 中为Electron邮件添加标题

Laravel 存储文件的公共 url

laravel 5.1 在没有 VM 重启的情况下看不到作业(job)文件的更改

使用哪个 Auth::check() 或 Auth::user() - Laravel 5.1?

日期时间格式无效:1366 字符串值不正确

在laravel中删除确认

Laravel如何仅响应没有正文消息的204代码状态