How can I export the value of {this.props.price * this.state.quantity} so I could use it in a different .js file // a different react component?

Viewed 24
import React, {Component} from 'react';
import { dumpLogs } from './Utils.js';



class RecCard extends Component {
     state = {
         quantity: null
     }

     incrementNumber = () => {
         this.setState((prevState, prevProp) => {
             return {quantity : prevState.quantity +1}
         });
     }

     decrementNumber = () => {
         this.setState((prevState, prevProp) => {
            return {quantity : prevState.quantity -1}
            
         });
     }

    render() {
    dumpLogs(this.props);
    return(
        <div>
            <h3>{this.props.title}</h3>
            <h5>{this.props.price} PLN</h5>
            <span>{this.state.quantity}</span> &nbsp; 
            <span className='hidden'>{this.props.price * this.state.quantity}</span> 
            <button onClick={this.incrementNumber}>+</button>
            <button onClick={this.decrementNumber} disabled={this.state.quantity <= null}>-
            </button>
        </div>

    )}
        
}



export default RecCard; 
1 Answers

From what i can see, this is (most likely) a duplicate of This Post.
You should be able to export values using the export keyword. By assigning your this.props.price * this.state.quantity to a local variable and then exporting said variable (using export MaxPrice, or whatever you called that variable).

Related