首页 > 解决方案 > 反应本机文本输入从另一个文本输入中获取值

问题描述

当我在textInput 商品编号中输入商品数量时,我希望商品的价格自动出现在textIinput Price中。这是我的快速脚本,它不起作用

请帮我改进,谢谢

    constructor(props){
        super(props);
        this.state={
            qty: 0, 
            prc: 0,
        }
    }

    price = () =>{
        var price = this.state.qty * 250;
        this.setState({ 
            prc: price,
        })
    }

    render(){
        return (
            <View style={styles.container}>
                .....
                        <View style={{...}}>
                            <Text style={{...}}>Item number :</Text>
                            <TextInput
                                ...
                                onChangeText={(entry) => {
                                    this.setState({qty: entry})
                                    this.price();
                                }}
                                ...
                            />
                        </View>
                        <View style={{...}}>
                            <Text style={{...}}>Price :</Text>
                            <TextInput
                                ....
                                value={this.state.prc}
                                onChangeText={(entry) => {
                                    this.setState({prc: entry})
                                }}
                                ....
                            />
                        </View>
            </View>

标签: react-native

解决方案


import React, { Component, useState } from 'react';

从'react-native'导入{视图,文本,文本输入};

导出默认示例 = () => {

const [price, setprice] = useState(0);
return (
    <View>
        <Text>Item number :</Text>
        <TextInput
            onChangeText={(entry) => setprice(entry)}
        />
        <Text>Price :</Text>
        <TextInput defaultValue={(parseInt(price) * 250).toString()}></TextInput>
    </View>
);

}


推荐阅读