首页 > 解决方案 > 如何通过点击按钮更改输入文本上的变量?反应原生

问题描述

该图像显示了代码的作用。当我点击 + 或 - 按钮时,我希望输入文本中的文本发生变化。我怎样才能做到这一点?

function Cerveja(props) {
const [count,setCount] = useState(0); 
 <View style={{flex: .1}}>
    <Button
        onPress={()=>setless(count*1)}
        title="-"
        color="#841584"
      />
  </View>
    <View style={{flex: 0.11}}>
    <TextInput     
      textAlign={'center'}
      style={{height: 40}}
      placeholder={'0'}
      onChangeText={(count) => setCount(count)}
      keyboardType={'numeric'}
    />
    </View>
    <View style={{flex: .1}}>
      <Button 
          onPress={()=> setCount(count*1+1)}
          title="+"
          color="#841584"
      />
    </View>
    <View style={{flex: .19}}>
      <Text textAlign={'center'}>R$ {parseFloat((count*1*props.price).toFixed(2))}</Text>
    </View>
</View>

图片

请帮忙!

标签: react-nativebuttontextinput

解决方案


这可能适用于“+”按钮

onPress={()=> setCount(count+1)}

这对于“-”按钮

onPress={()=> setCount(count-1)}

推荐阅读