简体   繁体   English

React-Native 改变 inputText 背景一秒钟

[英]React-Native Changing inputText background for a second

I am working on a React Native app and I have an issue.我正在开发一个 React Native 应用程序,但我遇到了一个问题。 I want to make inputText background green for just a second when user answers correctly.当用户正确回答时,我想让 inputText 背景变为绿色。 I do not use button so if user have correct answer, the inputText will be empty ('').我不使用按钮,所以如果用户有正确的答案,inputText 将为空('')。 I am using useState for this.我为此使用 useState 。 If the text is the correct answer, setWord('') is triggered.如果文本是正确答案,则触发 setWord('')。

const [word, setWord] = useState('');

Let's say the usual background-color is white and the correct background-color will be green.假设通常的背景颜色是白色,正确的背景颜色是绿色。

Add a background color state like this:像这样添加背景颜色 state:

const [textinputBackgroundColor, setTextinputBackgroundColor] = useState('white');

Now, wherever you check the correctness of the text - whenever the text is correct you can change the background color like this:现在,无论您检查文本的正确性 - 只要文本正确,您就可以像这样更改背景颜色:

setTextinputBackgroundColor('green');
setTimeout(() => setTextinputBackgroundColor('white'), 1000);

The timeout will revert the white background color after 1 second.超时将在 1 秒后恢复白色背景颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM