【问题标题】:How to sum React component values如何对 React 组件值求和
【发布时间】:2019-08-26 19:06:35
【问题描述】:

我是一个初学者,正在编写一个 React 应用程序,目前包含三个组件减去根应用程序(是的,这是针对在线课程的)。前两个并不重要,但对于第三个,名为 Total,我需要返回定义为三个常量的练习计数的总和,练习 1~3。

我被教导如何对道具求和,但仅限于 {props + props1 + props2...},我也可以这样做,但随着零件和练习数量的增加,它就不好了。我可以减少值或编写一个辅助函数,但是 React 的工作方式有点不同,我有点迷失了如何为此提供一个良好的可扩展解决方案。

//first two components hidden

const Total = (props) => {
    return (
        <>
            <p>help here!!</p>
        </>
    )
}

const App = () => {
    const course = "Half Stack app development"
    const part1 = "Fundamentals of React"
    const exercises1 = 10 //this one
    const part2 = "Using props to pass data"
    const exercises2 = 7 //this one
    const part3 = "State of a component"
    const exercises3 = 14 //and this one

    return (
        <div>
            <Header name={course} />
            <Content name={part1} exercises={exercises1} />
            <Content name={part2} exercises={exercises2} />
            <Content name={part3} exercises={exercises3} />

            <Total exercises= help here!! />
        </div>
    )
}

【问题讨论】:

  • 在你的 App 组件 part1, part2, part3 里面有 props 进来吗?还有练习1、练习2、练习3
  • 是的,课程变量用于Header组件中的props,parts用于Content组件中的props。练习1~3用于Content和Total。
  • 我的意思是它们是使用 App 组件从其他组件进来的,还是这只是您正在尝试的一个示例。无论如何,我已经发布了一个自定义解决方案,假设您可以完全访问代码的数据结构

标签: javascript reactjs


【解决方案1】:

您应该将数据构造为对象数组。

 const exercises = [{ name: 'Fundamentals of React', exercise: 10 }, etc];

在渲染中使用

exercises.map(ex => <Content name={ex.name} exercises={ex.exercise}/>)

总和使用 reduce https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

【讨论】:

    【解决方案2】:

    这是我的解决方案,假设您控制 App 组件中的数据结构

    const Total = (props) => {
        return (
            <>
                <p>props.totalExerciseCount</p>
            </>
        )
    }
    
    const App = () => {
        const course: {
          name: "Half Stack app development",
          parts: [
            {
              title: "Fundamentals of React"
              exerciseCount: 10
            },
            {
              title: "Using props to pass data"
              exerciseCount: 7
            },
            {
              title: "State of a component"
              exerciseCount: 14
            },
          ]
        }
    
      let totalExerciseCount = 0;
    
        return (
            <div>
                <Header name={course.name} />
                {course.parts.map(part => {
                    totalExerciseCount += part.exerciseCount;
                    return <Content name={part.title} exercises={part.exerciseCount} />
                  })
                }
    
                <Total exercises={totalExerciseCount} />
            </div>
        )
    }
    

    【讨论】:

      【解决方案3】:

      您可以创建一个Total 组件作为包装器并计算孩子的道具:

      const Total = (props) => {
          const { children } = props;
          const total = children.reduce((total, { props }) => total += props.exercises, 0);
      
          return (
              <>
                  {children}
                  <p>{total}</p>
              </>
          )
      }
      const App = () => {
          const course = "Half Stack app development"
          const part1 = "Fundamentals of React"
          const exercises1 = 10 //this one
          const part2 = "Using props to pass data"
          const exercises2 = 7 //this one
          const part3 = "State of a component"
          const exercises3 = 14 //and this one
      
          return (
              <div>
                  <Header name={course} />
                  <Total>
                    <Content name={part1} exercises={exercises1} />
                    <Content name={part2} exercises={exercises2} />
                    <Content name={part3} exercises={exercises3} />
                  </Total>
              </div>
          )
      }
      export default App;
      
      

      【讨论】:

      • 而且这个解决方案可以扩展
      • 此解决方案存在不必要的风险。对Content 组件的任何更改都会破坏Total 组件,因为它对其子组件了解太多。最好使用某种ContentList 使其面向数据,它接收整个数据作为道具并呈现子项和总数。
      • @EmileBergeron,是的,我同意你的看法。使Total 组件更加独立的另一种可能的方法是将sumByPropName="exercises" 传递给Total 并以这种方式使用它const total = children.reduce((total, { props }) =&gt; total += props[sumByPropName], 0)
      【解决方案4】:
      const Total = (props) => {
          return (<p>{props.exercises}</p>)
      }
      

      然后

      <Total exercises={exercises1 + exercises2 + exercises3} />
      

      【讨论】:

      • OP 说他们不想这样做并试图避免
      • 从描述(在线课程)来看,我猜这是他们应该这样做的方式,后来他们用状态和传递函数对其进行重构,使其更具动态性
      猜你喜欢
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-28
      相关资源
      最近更新 更多