как разместить реквизит - PullRequest
       30

как разместить реквизит

0 голосов
/ 20 февраля 2020

Я хотел бы объяснить мою проблему дня.

Я не могу опубликовать "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)

У вас есть идеи, как это исправить? Neff

1 Ответ

3 голосов
/ 20 февраля 2020

Вы пытаетесь структурировать {this.props.total}, что является недопустимым синтаксисом.

Вы можете передать объект явно, определяя ключ total, например:

body: JSON.stringify({total: this.props.total}),

Или просто зафиксируйте сам объект this.props:

body: JSON.stringify(this.props),
...