Search code examples
javascriptreactjsrefs

How to use Refs Properly in React js?


I am confused with how the react refs works. The issue for me is, whenever I change the input select value, update_cart function is called.

I then call actions to set the value using relevant APIs.

However, currently, whenever I change the value, the whole component refreshes and the refs value are set to undefined.

What am I doing wrong?

Please note I have included only relevant codes.

    /** @jsx React.DOM */
    'use strict'

    var React = require('react')
    var connect = require("react-redux").connect
    var moment = require('moment')
    var actions=require("../actions");
    var Calendar=require("./calendar");
    var utils=require("../utils");
    var CartChangeQty = require('./cart_change_qty')
    var Table = require('react-bootstrap').Table
    var Input = require('react-bootstrap').Input
    var Register = require('./register')
    var GroceryCheckout = React.createClass({
        getInitialState: function() {
            return {provinces: [], postal_codes: []};
        },

        render: function() {
            console.log("GroceryCheckout.render");
            var day_slots=[];
            if (this.props.grocery_cart) {
                var delivery_date=this.props.grocery_cart.delivery_date;
                if (!delivery_date) delivery_date=this.props.grocery_cart.delivery_slots[0][0];
                _.each(this.props.grocery_cart.delivery_slots,function(obj) {
                    if (obj[0]==delivery_date) {
                        day_slots=obj[1];
                    }
                }.bind(this));
                console.log("###### day_slots",day_slots);
            }
            return <div className="plr-grocery-checkout">
                <a className="plr-anchor" id="checkout"></a>
                <h2>Grocery Checkout</h2>
                {function() {
                    if (!this.props.grocery_cart) return <p>Your grocery cart is empty.</p>;
                    if (!this.props.user_data) {
                        return <div>
                            <p>
                                Is this your first time ordering? <input type="radio" name="first_time" ref="first_time_yes" onClick={this.onchange_first_time.bind(this,true)}/> Yes <input type="radio" name="first_time" ref="first_time_no" onClick={this.onchange_first_time.bind(this,false)}/> No
                            </p>
                            {function() {
                                if (this.state.first_time==true) {
                                    return <Register/>
                                } else if (this.state.first_time==false) {
                                    return ///something
                    } else {
                        return <div>
                            ///something
                             <div>
                            <h4><i className="glyphicon glyphicon-home"> </i> Delivery Address</h4>
                            <Input type="select" onChange={this.update_cart} ref="ship_address" style={{width:'auto',padding:'inherit',height:'auto'}}>
                                {this.props.user_data.contact_id.addresses.map(function(obj) {
                                    return <option key={obj.id} value={obj.id}>{obj.address}</option>
                                })}
                            </Input>

                            <h4><i className="glyphicon glyphicon-calendar "> </i> Please select your preferred delivery time slot:</h4>
                            <Calendar />

                                <div className="form-group">
                                    <label className="col-sm-2 control-label">Payment Method</label>
                                    <div className="col-sm-6">
                                        <Input type="select" onChange={this.update_cart} ref="pay_method" style={{width:'auto',padding:'inherit',height:'auto'}}>
                                            {this.props.grocery_cart.payment_methods.map(function(obj) {
                                                console.log("********************** payment method",obj.name)
                                                return <option key={obj.id} value={obj.id}>{obj.name}</option>
                                            }.bind(this))}
                                        </Input>
                                    </div>
                                </div>
                                <div className="form-group">
                                    <label className="col-sm-2 control-label">Payment Amount</label>
                                    <div className="col-sm-6">
                                        <p>{this.props.grocery_cart.amount_total} ฿</p>
                                    </div>
                                </div>
                            </div>
                            <hr/>
                            <h4><i className=" glyphicon glyphicon-list-alt"> </i>  Other Information</h4>
                            <div className="form-horizontal">
                                <div className="form-group">
                                    <label className="col-sm-2 control-label">Return Foam Box</label>
                                    <div className="col-sm-6">
                                        <input type="checkbox"  onChange={this.update_cart}/>
                                        <span style={{margin:10}}>For this delivery, would you like us to take back the foam box for recycling?</span>
                                    </div>
                                </div>
                            </div>
                            <div className="form-horizontal">
                                <div className="form-group">
                                    <label className="col-sm-2 control-label">No Call</label>
                                    <div className="col-sm-6">
                                        <input type="checkbox" onChange={this.update_cart}/>
                                        <span style={{margin:10}}>For this delivery, please do NOT call me one hour before delivery to re-confirm unless delayed</span>
                                    </div>
                                </div>
                            </div>
                            <button className="btn btn-lg btn-primary" onClick={this.send_order} disabled={this.props.grocery_cart.amount_total<1500?true:false}><span className="glyphicon glyphicon-ok"></span> Send Order</button>
                            {this.props.sending_grocery_order?<span><img src="/static/img/spinner.gif"/> Sending order...</span>:null}
                            {function() {
                                if (this.props.grocery_cart.amount_total>=1500) return;
                                return <span className="plr-warning" style={{marginLeft:"20px"}}>Min. order: 1500 ฿!</span>
                            }.bind(this)()}
                        </div>
                    }
                }.bind(this)()}
            </div>
        },

        onchange_first_time: function(value) {
            console.log("GroceryCheckout.onchange_first_time",value);
            this.setState({first_time: value});
        },

        update_cart: function() {
            console.log("GroceryCheckout.update_cart");
            console.log("this.refs.pay_method.value",this.refs.pay_method.value);
            var vals={
                customer_id: this.props.user_data.contact_id.id,
                ship_address_id: this.refs.ship_address.value||null,
                bill_address_id: this.refs.bill_address.value||null,
                pay_method_id: parseInt(this.refs.pay_method.value),
            };
            this.props.dispatch(actions.grocery_cart_update(vals));
        },



        onchange_qty: function(product_id,qty) {
            console.log("GroceryItem.onchange_qty",product_id,qty);
            this.props.dispatch(actions.grocery_cart_set_qty(product_id,qty));
        },


    })

    var select=function(state) {
        return {
            grocery_cart: state.grocery_cart,
            grocery_cart_loading: state.grocery_cart_loading,
            user_data: state.user_data,
            user_data_loading: state.user_data_loading,
            sending_grocery_order: state.sending_grocery_order,
        }
    }

    module.exports=connect(select)(GroceryCheckout);

Solution

  • The input i am using is a react bootstrap

    <Input type="select" onChange={this.update_cart} ref="ship_address" style={{width:'auto',padding:'inherit',height:'auto'}}>
       {this.props.user_data.contact_id.addresses.map(function(obj) {
           return <option key={obj.id} value={obj.id}>{obj.address}</option>
       })}
    </Input>
    

    The problem was whenever the onchange was called the render method was again called. It rerenders because i am calling the set state in here.

    One way to fix the issue is to put the input into a different component , where it just re renders that component on change .

    Cheers.