简体   繁体   中英

how to change object state value in javascript (react-native)

i'am using backendless api to create simple crud application. this is my table schema:

products table:

  • name (string)
  • price (double)
  • description (text)

i use input component from native base to get the value from user. But i have problem with price, when i input price 5000 then in the database the value is not 5000 but 892350512, i think this is happen because the value from onChangeText is string and i confuse how to parseInt() in my state.

this is my code:

state = { data: {} }

allProduct(){
    axios.get(`${uri}/products?sortBy=created%20desc`).then(result => {
        this.setState({
            data: result.data
        })
    })
}

handleSubmit(){ 

    axios.post(`${uri}/products`, this.state.data).then(result => {
        if(result.data){
            this.allProduct,
            alert("Succes!")
        }
    })

    // alert(JSON.stringify(this.state.data))
}

And this is my form code to get value from user:

<Label style={styles.batasAtas}>Harga</Label>
<Item regular>
    <Input onChangeText={(price) => this.setState({data:{...this.state.data, price}})} keyboardType = 'numeric'/>
</Item>

You can do:

<Input onChangeText={(price) => this.setState({data:{...this.state.data, parseInt(price)}})} keyboardType='numeric'/>

To change the price to an integer.

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