【问题标题】:Value is not changing in React-Native-Numeric-InputReact-Native-Numeric-Input 中的值没有变化
【发布时间】:2019-05-28 12:12:52
【问题描述】:

我正在为我的学习目的制作一个电子商务应用程序。我在购物车页面中使用 textInput 更新数量现在我正在使用 react-native-numeric-input 。

我正在更新 react-native-numeric-input 的 onChange 数量,当我正常测试它时它正在工作,但是当我添加我的代码以更新我的数据库中的数量时,它无法正常工作...... 这里我附上应用程序异常行为的视频: Visit link to watch video

这是我的数字输入组件代码:

<NumericInput
    totalHeight={35}
    totalWidth={80}
    validateOnBlur={false}
    initValue={qtyN}
    separatorWidth={0}
    rounded={true}
    textColor='#2e6153'
    borderColor='#0000'
    inputStyle={{
        backgroundColor: "#fff",
        borderWidth: 1,
        borderColor: '#2e6153',
        borderStyle: 'solid',
    }}
    containerStyle={{
        backgroundColor: '#2e6153',
        borderWidth: 1,
        borderColor: '#2e6153',
        borderStyle: 'solid',
    }}
    iconStyle={{
        color: '#fff',
    }}
    leftButtonBackgroundColor='#2e6153'
    rightButtonBackgroundColor='#2e6153'
    minValue={1}
    maxValue={999}
    onChange={(value) => this.qtyNumHandle(value, data.item.id)}
/>

和我在 onChange 上使用的函数:

qtyNumHandle = async (value, id) => {
    console.log("qtyNum handler");
    console.log(value)
    console.log(id)
    try{
        let user_id = await AsyncStorage.getItem(ID);
        console.log(user_id);
        let updateProduct = await fetch(`http://website.com/api/cart/updatecart?productid=${id}&userid=${user_id}&qty=${value}`, {
            "method": "POST",
            "headers": {
                "cache-control": "no-cache",
            },
        });
        let updateProductRes = await updateProduct.json();
        console.log(updateProductRes);
        this.fetchCartProduct();
    }catch(errors){
        console.log(errors);
    }
}

如果我做错了什么,请告诉我。如果我的代码是正确的,为什么会出现这个问题。

【问题讨论】:

    标签: react-native react-native-android


    【解决方案1】:

    是的..!!我得到了解决方案。

    问题是我没有使用 value 属性。

    这是我的 NumericInput 新代码:

    <NumericInput
        value={this.state.cartTotal[data.index].qty}
        // above the state I've used is containing the array data of my cart products
        // and I used [data.index] because this NumericInput is in FlatList.
        // so to get the quantity of perticular I used it array[data.index].qty
        totalHeight={35}
        totalWidth={80}
        validateOnBlur={false}
        initValue={this.state.cartTotal[data.index].qty}
        separatorWidth={0}
        rounded={true}
        textColor='#2e6153'
        borderColor='#0000'
        inputStyle={{
            backgroundColor: "#fff",
            borderWidth: 1,
            borderColor: '#2e6153',
            borderStyle: 'solid',
        }}
        containerStyle={{
            backgroundColor: '#2e6153',
            borderWidth: 1,
            borderColor: '#2e6153',
            borderStyle: 'solid',
        }}
        iconStyle={{
            color: '#fff',
        }}
        leftButtonBackgroundColor='#2e6153'
        rightButtonBackgroundColor='#2e6153'
        minValue={1}
        maxValue={999}
        onChange={(value) => this.qtyNumHandle(value, data.item.id)}
    />
    

    这是我在 onChange 上使用的函数(没有变化):

    qtyNumHandle = async (value, id) => {
        console.log("qtyNum handler");
        console.log(value)
        console.log(id)
        try{
            let user_id = await AsyncStorage.getItem(ID);
            console.log(user_id);
            let updateProduct = await fetch(`http://website.com/api/cart/updatecart?productid=${id}&userid=${user_id}&qty=${value}`, {
                "method": "POST",
                "headers": {
                    "cache-control": "no-cache",
                },
            });
            let updateProductRes = await updateProduct.json();
            console.log(updateProductRes);
            this.fetchCartProduct();
        }catch(errors){
            console.log(errors);
        }
    }
    

    希望这对其他人有帮助。

    【讨论】:

    • 我遇到了类似的问题,但在我的情况下,当我使用带有道具的值时,从 firebase 值获取数据时,单击 + 和 - 按钮时既不会增加也不会减少。当 value prop 不与获取的数据一起使用时,它工作正常。但出于我的目的,需要使用获取的数据更改 numeric-input 的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 2018-05-06
    • 2020-08-05
    • 2017-03-30
    • 1970-01-01
    相关资源
    最近更新 更多