简体   繁体   中英

how post props redux React

I would like to explain my problem of the day.

I can't post "this.props.total", I do not understand how to post a props, can you help me pls? currently the props works correctly.

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)

Do you have an idea of how to fix this? Neff

You are attempting to stringify {this.props.total} , which is invalid syntax.

You can pass an object explicitely defining the total key like so:

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

Or, simply stringify the this.props object itself:

body: JSON.stringify(this.props),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM