I am trying to create a simple question-answer app, I shared the example of my code below, on the below example how can I hide the Answer by default and show the answer when I click on the button?
import React, { useEffect, useState } from "react";
import {View, Text, StyleSheet, Button} from 'react-native'
const Testing = ({ navigation }) =>{
return (
<View style={{
backgroundColor: '#f1f1f1',justifyContent: 'center', alignItems: 'center',}}>
<Text>1. In which of the following languages is function overloading not possible?</Text>
<Text>
1. C
2. C++
3. Java
4. Python
</Text>
<Text>Answer: C</Text>
<Button title="Show Answer"/>
<Text>2.What is Lorem Ipsum?</Text>
<Text>
1. Lorem
2. Ipsum
3. dummy
4. text
</Text>
<Text>Answer: Lorem</Text>
<Button title="Show Answer"/>
</View>
);}
export default Testing;
3
Answers
Add state showAnswer, conditional render and button click event handler
To achieve this effect I recommend you use a state with a boolean type, so
true
orfalse
Like this:
then on the area where you want the text to render use a Ternary Operator
When the
showValue
state is true React will render out the textthen give the button an
OnClick()
Like so
Which will toggle the state between
true
andfalse
One way of doing this is to have each pair of question and answer as component:
and the component would look something like this