使用React.js在if语句中将变量值作为条件传递

时间:2019-07-05 08:47:37

标签: javascript html reactjs

我是一名学生,我正在尝试使用React.js进行电子商务作业的购物车摘要,以进行作业。 我需要根据所选的单选按钮设置运费(运费有两个选项:第一个是 free ,第二个是 $ 20 ),但是我无法做到。我认为使用if语句可以实现正确的方法,但是我不确定。有人可以指导我如何实现这一目标吗?

这是按钮所在页面的代码:

        import React, {Component} from 'react';
        import {Col, Row, Button, Form, Card} from 'react-bootstrap';
        import {Link} from 'react-router-dom';
        import '../styles/shopping.css';
        import Header from '../components/header';
        import Foot from "../components/foot";
        import Summary from '../components/summary';

        class Shipping extends Component{    
            constructor(props) {
                super(props);
                this.state = {
                    activeIndex: ''
                };        
                this.radioButtons = [            
                    {text: 'Free Shipping \n\r Between 2 - 5 working days'},
                    {text: 'Next Day Delivery - $20 \n\r 24 hours from checkout'} 
                    ]
                }      
                updateRadioGroup(i) {
                  this.setState({activeIndex: i})
                }        
            render(){
                let { activeIndex } = this.state;     
                return(
                                <Row>
                                    <Col xs={6} id="ship-sx">
                                    {
                                    this.radioButtons.map((element,i ) => (
                                        <Card className="btn-shipping" class="boxClickCss" key={i} className={activeIndex === i ? 'btn-shipping actv' : 'btn-shipping'}>
                                            <Row>
                                                <Col xs={2} className="center-radio">
                                                    <label className="container-btn-radio">
                                                        <input required type="radio" name="ship" onClick={() => this.updateRadioGroup(i)}/>
                                                        <span className="checkradio"/>
                                                    </label>
                                                </Col>                                        
                                                <Col xs={10}>
                                                    <div style={{maxWidth:"55%"}}>{element.text}</div>
                                                </Col>
                                            </Row>
                                        </Card>
                                    ))
                                    }
                                    </Col>
                                    <Col xs={12} md={4}>
                                        <Summary/>
                                    </Col>
                                </Row>

            )
        }
    }
    export default Shipping;

这是页面中包含的组件,必须直观显示运费:

import React from 'react';
import {Col, Row, Accordion, Dropdown, Button} from 'react-bootstrap';
import Coupon from "./coupon";
import SummaryProd from "./summaryProd";
import '../styles/summary.css';

function somma() {
    var z = (localStorage.getItem('price') +20 +20);
    return z;
}
var risultato = somma();

export const  showProduct=()=>{        
    localStorage.getItem('name');
    localStorage.getItem('price');
    localStorage.getItem('description');
    localStorage.getItem('img');
}
export const Summary = () => (             
    (localStorage.getItem('id') !== null) ? ( 
        <>
        <Row>
            <Col id="summary-col">
                <div>
                    <h3>Summary</h3>
                        <hr className='row-shopping'/>
                    </div>                                 
                    <SummaryProd/>
                    <Coupon/>
                    <hr className='row-shopping'/>
                    <div>
                    <Accordion defaultActiveKey="0">
                        <Accordion.Toggle id="btn-voucher" as={Button} variant="link" eventKey="1" split>
                            <span>HAVE A VOUCHER?</span><Dropdown.Toggle id="voucher-arrow"/>
                        </Accordion.Toggle>                        
                        <Accordion.Collapse eventKey="1">
                        <input/>
                        </Accordion.Collapse>
                    </Accordion>
                    </div>
                    <hr className='row-shopping'/>
                    <div className="summary-details">
                        <span>
                            SUBTOTAL
                        </span>
                        <span>
                            ${localStorage.getItem('price')}
                        </span>
                    </div>
                    <div className="summary-details">
                        <span>
                            SHIPPING
                        </span>
                        <span>
                            {/* SHIPPiNG COST */}
                        </span>
                    </div>
                    <div className="summary-details">
                        <span>
                            TAXES
                        </span>
                        <span>
                            $20
                        </span>
                    </div>
                    <hr className='row-shopping'/>
                    <div className="summary-details">
                        <span>
                            TOTAL
                        </span>

                        <span>

                        </span>
                    </div>
                </Col>
            </Row>
            </>
        ):(
        <Row style={{height: '50vh'}}>
            <Col id="summary-col">
                <div >
                    <h3>Summary</h3>
                    <hr className='row-shopping'/>
                    <span>Your cart is empty!</span>
                </div>
            </Col>
        </Row>           
    )               
)

export default Summary;

1 个答案:

答案 0 :(得分:0)

您可以这样做

constructor(props) {
    super(props);
    this.state = {
        activeIndex: '',
        shippingCost: '',
    };  
    //set cost here only      
    this.radioButtons = [            
        {text: 'Free Shipping \n\r Between 2 - 5 working days', cost: 'Free'},
        {text: 'Next Day Delivery - $20 \n\r 24 hours from checkout', cost: '$20'} 
    ]
}      
updateRadioGroup(i) {
    let shippingCost = this.radioButtons[i].cost;
    this.setState({activeIndex: i, shippingCost }) //set shippingCost in state
}        

设置运费后,您需要将其传递给Summary组件,

<Summary shippingCost={this.state.shippingCost} /> 

Summary组件中的用法,

export const Summary = (props) => (   //your props passed 
 ...
 <div className="summary-details">
    <span>
       SHIPPING
    </span>
    <span>
       {props.shippingCost}
    </span>
</div>
...
)

Demo,且代码减少。