Home > OS >  react native : change component props on press
react native : change component props on press

Time:05-02

Im trying to change the props of the button which is clicked : if clicked more than 3 times, i want to disable it.

Here is my code :

import React, { useState } from 'react';
import { Button, Text, View } from 'react-native';

const App = () => {
  const [pressedCount, setPressedCount] = useState(0);

  return (
    <View style={{ flex: 1, justifyContent: 'center' }}>
      <Text style={{ margin: 16 }}>
        {pressedCount > 0
          ? `The button was pressed ${pressedCount} times!`
          : 'The button isn\'t pressed yet'
        }
      </Text>
      <Button
        title='Press me'
        onPress={() => if (pressedCount>=3){Button.setState.({disabled:true})} else{setPressedCount(pressedCount 1)}}
      />
    </View>
  );
};

export default App;

I've tried if (pressedCount>=3){Button.props.disabled=true} else{setPressedCount(pressedCount 1)}} but it's the same.

Any advice on how to properly do that?

CodePudding user response:

<Button
  title="Press me"
  onPress={() => setPressedCount((prevState) => prevState   1)}
  disabled={pressedCount > 3}
/>;
  • Related