我想解释一下我今天的问题。
我无法发布“ this.props.total”, 我不知道如何发布道具,您能帮我吗? 目前道具正常工作。
import React, { Component } from 'react';
import { CardText, } from 'reactstrap';
import { connect } from 'react-redux'
class thisPropsFortotal extends Component {
handleSubmit = (e) => {
e.preventDefault();
const config = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({this.props.total}),
};
const url = entrypoint + "/alluserpls";
fetch(url, config)
.then(res => res.json())
.then(res => {
if (res.error) {
alert(res.error);
} else {
alert(`ajouté avec l'ID ${res}!`);
}
}).catch(e => {
console.error(e);
}).finally(() => this.setState({ redirect: true }));
}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<button type="submit">Add</button>
</form>
<CardText>{this.props.total} € </CardText>
</div>
);
}
}
const mapStateToProps = (state) => {
return {
total: state.addedItems.reduce((acc, item) => { return acc + (item.quantity *
item.price) }, 0)
//addedItems: state.addedItems
}
}
export default connect(mapStateToProps)(thisPropsFortotal)
您是否知道如何解决此问题?内夫
答案 0 :(得分:3)
您正在尝试对{this.props.total}
进行字符串化,这是无效的语法。
您可以像这样传递一个显式定义total
键的对象:
body: JSON.stringify({total: this.props.total}),
或者,只需对this.props
对象本身进行字符串化:
body: JSON.stringify(this.props),