【问题标题】:react parent component add up values from child not accurate反应父组件从子组件加起来不准确
【发布时间】:2018-06-24 08:24:36
【问题描述】:

嘿,我有一个可重用的列表组件,我可以在其中添加项目并添加价格总和,并且我希望将这个组件放在一个父组件中,在那里我有总和和一个添加更多列表的按钮。 我怎样才能得到总和? 到目前为止,我得到了它们并将它们添加到一个数组中,但这些值不是它们应该的值。

代码:

import React from 'react';
import styled from 'styled-components';
import ShoppingList2 from './ShoppingList2';

const Container = styled.div `
  position:absolute;
  left:0;
  bottom:0;
  right:0;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
`;

class TotalPrice extends React.Component {
  state = {
    shoppingLists2: [ShoppingList2],
    shoplistsums: [],
    sum: 0
  }

  AddShoppingList = () => {
    let shoppingLists2 = this.state.shoppingLists2.concat(ShoppingList2);
    this.setState({
      shoppingLists2
    })
    console.log(this.state.shoppingLists2);
  }

  onUpdate = (val) => {
    console.log('val', val);
    let shoplistsums = this.state.shoplistsums.concat(val);
    console.log('shpl', shoplistsums);
    let sum = shoplistsums.reduce(function(a, b) {
      return a + b;
    });
    this.setState({
      shoplistsums,
      sum
    })
  }

  render() {
    return (
      <div>
        {this.state.shoppingLists2.map((shoppingList, index)=>(
          <ShoppingList2
            key={index}
            value={this.onUpdate}
          />
        ))}
        <Container>
          <label>Total:</label>
          <label>{this.state.sum + ' €'}</label>
          <button onClick={this.AddShoppingList}>Add Receipt</button>
        </Container>
      </div>
    );
  }
}

export default TotalPrice;

子组件

import React from 'react';
import styled from 'styled-components';
import InputFields from './InputFields';

const Container = styled.div `
  display: flex;
  flex-direction: row;
  justify-content: space-between;
`;

class ShoppingList extends React.Component {

  state = {
    questions: [''],
    sum: 0
  }

  handleText = i => e => {
    let questions = [...this.state.questions];
    questions[i] = parseInt(e.target.value);
    if (questions.length === 1 && isNaN(questions[0])) {
      questions[0] = ''
    }
    let sum = questions.reduce(function(a, b) {
      if (isNaN(a)) {
        a = '';
      }
      if (isNaN(b)) {
        b = '';
      }
      return a + b;
    });
    console.log('sum', sum);
    this.props.value(sum);
    this.setState({
      questions,
      sum
    })
  }

  addExpense = e => {
    e.preventDefault()
    let questions = this.state.questions.concat([''])
    this.setState({
      questions
    })
  }

  render() {
    return (
      <div>
        <Container>
          <select>
            <option value="food">Food</option>
            <option value="houseware">Houseware</option>
            <option value="entertainment">Entertainment</option>
          </select>
          <button onClick={this.addExpense}>Add expense</button>
        </Container>
        {this.state.questions.map((question, index) => (
          <Container  key={index}>
            <input
              type="text"
            />
            <input
              type="number"
              onChange={this.handleText(index)}
              value={question}

            />
          </Container>
        ))}
        <Container>
          <label>Total:</label>
          <label>{this.state.sum ? this.state.sum + ' €' : 0 + ' €'}</label>
        </Container>
      </div>
    )
  }
}

export default ShoppingList;

【问题讨论】:

    标签: javascript reactjs parent-child state


    【解决方案1】:
    {this.state.shoppingLists2.map((shoppingList, index)=>(
      <ShoppingList2
        key={index}
        value={this.onUpdate}
      />
     ))}
    

    这里您使用了多个购物清单组件,但只呈现您已导入的那个。而不是这个使用这个

    {this.state.shoppingLists2.map((ShoppingList, index)=>(
      <ShoppingList
        key={index}
        value={this.onUpdate}
      />
     ))}
    

    下一个问题是您从子组件发送值(总和)。当你应该根据你的代码对单个数字求和时,对总和求和。

    我修改了一些你的代码。并根据组件的索引存储值(总和)。最后把它们加起来

    这是完整的组件

    import React from "react";
    import ReactDOM from "react-dom";
    import styled from "styled-components";
    import ShoppingList2 from "./ShoppingList2";
    
    const Container = styled.div`
      position: absolute;
      left: 0;
      bottom: 0;
      right: 0;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
    `;
    
    class TotalPrice extends React.Component {
      state = {
        shoppingLists2: [ShoppingList2],
        shoplistsums: {},
        sum: 0
      };
    
      AddShoppingList = () => {
        let shoppingLists2 = this.state.shoppingLists2.concat(ShoppingList2);
        this.setState({
          shoppingLists2
        });
        console.log(this.state.shoppingLists2);
      };
    
      onUpdate = index => val => { // changed here
        console.log("val", val);
        let shoplistsums = { // and here
          ...this.state.shoplistsums,
          [index]: val
        };
        console.log("shpl", shoplistsums);
        // and this block, using reduce would do as well, but I like this method better
        let sum = 0; 
        Object.keys(shoplistsums).forEach(key => {
          sum += shoplistsums[key];
        });
        this.setState({
          shoplistsums,
          sum
        });
      };
    
      render() {
        return (
          <div>
            {this.state.shoppingLists2.map((ShoppingList, index) => (
              <ShoppingList key={index} value={this.onUpdate(index)} /> /* changed here */
            ))}
            <Container>
              <label>Total:</label>
              <label>{this.state.sum + " €"}</label>
              <button onClick={this.AddShoppingList}>Add Receipt</button>
            </Container>
          </div>
        );
      }
    }
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<TotalPrice />, rootElement);
    

    【讨论】:

    • 欣赏,但这里的总数还是一样:imgur.com/a/RcdKRzM 基本上需要以某种方式识别小列表,但是如何?
    猜你喜欢
    • 1970-01-01
    • 2019-12-04
    • 2021-04-27
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 2018-10-05
    相关资源
    最近更新 更多