0

I want to pass the function 'handleTaxInput' to value prop of input field to set the value of input field to whatever this function returns. But, on console.log(this.state.totalTaxInv) i'm getting undefined. I'm not getting actual state which i should be getting after running this.handleTaxAmount().

handleTaxAmount(){
        if(this.state.cgstsgstRate === 0 || this.state.cgstsgstRate === ""){
            return Number(this.state.amount) + Number(this.state.amount) * Number(this.state.igstRate / 100)
        } else if(this.state.igstRate === 0 || this.state.igstRate === ""){
            return Number(this.state.amount) + Number(this.state.amount) * Number(this.state.cgstsgstRate * 2 / 100)
        } else {
            return 0
        }
    }
                        <div>
                            <label>Total Inv. Amount</label>
                            <input name="totalInvAmt" placeholder={this.handleTaxAmount()} value={this.handleTaxAmount()}/>
                        </div>

I have already initialised all states using constructor .

6
  • Can you show us the full component? It’s not clear what you are trying to do, but you are not setting the state in any of the code in your question, so it’s not possible to see why you are expecting this.state.totalTaxInv to have a value Commented Jan 9, 2021 at 10:09
  • I have intialised states using constructor. Commented Jan 9, 2021 at 10:10
  • try with this.handleTaxAmount().bind(this) Commented Jan 9, 2021 at 10:12
  • But your handleTaxAmount function only returns a value, it doesn’t set the value to the state. So I can’t see why you are expecting the state to change? Did you want the handleTaxAmount function to update the state? Commented Jan 9, 2021 at 10:24
  • yes. I want the handleTaxAmount function to update the state. Can you provide a way for this? Commented Jan 9, 2021 at 10:30

1 Answer 1

1

I can't comment, so I'll have to ask this way. Did you initialize the state using this.setState(...) or did you just assign the state like this.state = {...}?

It seems like you want the state to change after calling your function, but you don't set the state anywhere in your function with this.setState(...).

Also: Why are you using so many Number constructors?

Edit (Answer):

handleTaxAmount() {
    let result = 0;

    if (this.state.cgstsgstRate === 0 || this.state.cgstsgstRate === "") {
        result = Number(this.state.amount) + Number(this.state.amount) * Number(this.state.igstRate / 100);
    } else if (this.state.igstRate === 0 || this.state.igstRate === "") {
        result = Number(this.state.amount) + Number(this.state.amount) * Number(this.state.cgstsgstRate * 2 / 100);
    }

    this.setState({
        variableYouWantToChange: result
    });
}

And if you want the value of your input field to change with the state use:

<div>
    <label>Total Inv. Amount</label>
    <input name="totalInvAmt" placeholder={this.state.variableChangedByFunction} value={this.state.variableChangedByFunction}/>
</div>

If you want to bind every change of the input to run the function do:

<div>
    <label>Total Inv. Amount</label>
    <input name="totalInvAmt" placeholder={this.state.variableChangedByFunction} value={this.state.variableChangedByFunction} change={this.handleTaxAmount}/>
</div>

and run this.handleTaxAmount = this.handleTaxAmount.bind(this) in your constructor.

If you want more information about how to use a components state see the docs

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I want to the state to change after calling the function.I have initialised state as this.state = {...}
I wanted the return value as Number.But, I was getting them as a string. so i change them to number.
Ok. Try ensuring that the properties in your state are always numbers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.