Javascript React显示子组件的总和值不准确
我试图显示所有子组件列表值的总和,但它不准确,因为它也保留了数组中的前一个值,我如何解决这个问题 守则:Javascript React显示子组件的总和值不准确,javascript,arrays,reactjs,components,Javascript,Arrays,Reactjs,Components,我试图显示所有子组件列表值的总和,但它不准确,因为它也保留了数组中的前一个值,我如何解决这个问题 守则: import React from 'react'; import styled from 'styled-components'; const Container = styled.div ` display: flex; flex-direction: row; justify-content: space-between; `; class ShoppingList e
import React from 'react';
import styled from 'styled-components';
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 => {
console.log('id', this.state.id);
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;
以及:
下面是它的外观和工作原理以及问题:
我最近才开始与react合作,所以如果你有其他建议,请告诉我 修复,您可以签出@-修复,您可以签出@-看起来问题是一个字符串数组。如何通过将字符串数组相加得到总和?若问题的状态是一个对象带有questionstring和valuenumber,然后为sum应用程序添加ValuesMembers.questions[i]=parseInte.target.value;它在ShoppingList中进行解析您的设计中应该有什么问题。食物、家庭用品和娱乐?或者它们是类别?看起来问题是一个字符串数组。如何通过将字符串数组相加得到总和?若问题的状态是一个对象带有questionstring和valuenumber,然后为sum应用程序添加ValuesMembers.questions[i]=parseInte.target.value;它在ShoppingList中进行解析您的设计中应该有什么问题。食物、家庭用品和娱乐?或者它们是类别?
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);
console.log(shoppingLists2);
this.setState({
shoppingLists2
})
}
onUpdate = (val) => {
console.log('val', val);
let shoplistsums = this.state.shoplistsums.concat(val);
let sum = shoplistsums.reduce(function(a, b) {
return a + b;
});
this.setState({
shoplistsums,
sum
})
}
render() {
return (
<div>
{this.state.shoppingLists2.map((ShoppingList2, 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;