react native:按下时更改组件道具
react native : change component props on press
我正在尝试更改被点击按钮的属性:如果点击超过 3 次,我想禁用它。
这是我的代码:
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;
我试过 if (pressedCount>=3){Button.props.disabled=true} else{setPressedCount(pressedCount+1)}} 但还是一样。
关于如何正确执行此操作的任何建议?
<Button
title="Press me"
onPress={() => setPressedCount((prevState) => prevState + 1)}
disabled={pressedCount > 3}
/>;
我正在尝试更改被点击按钮的属性:如果点击超过 3 次,我想禁用它。
这是我的代码:
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;
我试过 if (pressedCount>=3){Button.props.disabled=true} else{setPressedCount(pressedCount+1)}} 但还是一样。
关于如何正确执行此操作的任何建议?
<Button
title="Press me"
onPress={() => setPressedCount((prevState) => prevState + 1)}
disabled={pressedCount > 3}
/>;