How to get a user’s choice with onPress props in React NativeWhat is React Native method of getting user’s input like React’s e.target.innerTextWhat is the difference between state and props in React?Hide keyboard in react-nativeWhat is the difference between using constructor vs getInitialState in React / React Native?How to pass props to this.props.childrenWhat is the difference between React Native and React?React Native child overflowing when inside parent with flex:1React Native - image pushes text out screen - fixed with 3rd view?Component won't stay wrapped within bounds (React Native)Not able to navigate to other page in react nativeHow to update styles on child component onPress in react native
What is the shape of the upper boundary of water hitting a screen?
How was the website able to tell my credit card was wrong before it processed it?
Why do people prefer metropolitan areas, considering monsters and villains?
NOLOCK or Read Uncommitted locking / latching behaviours
I'm feeling like my character doesn't fit the campaign
Which is a better conductor, a very thick rubber wire or a very thin copper wire?
Intern not wearing safety equipment; how could I have handled this differently?
What was the significance of Spider-Man: Far From Home being an MCU Phase 3 film instead of a Phase 4 film?
Computer name naming convention for security
Why do most airliners have underwing engines, while business jets have rear-mounted engines?
What does "spinning upon the shoals" mean?
In layman's terms, does the Luckstone just give a passive +1 to all d20 rolls and saves except for death saves?
Difference between [[ expr1 || expr2 ]] and [[ expr1 ]] || [[ expr2 ]]
Why did RFK loathe LBJ?
What exactly is a "murder hobo"?
What does the multimeter dial do internally?
Examples of fluid (including air) being used to transmit digital data?
Why no parachutes in the Orion AA2 abort test?
Gory anime with pink haired girl escaping an asylum
What is this burst transmission sequence across the entire band?
What are some bad ways to subvert tropes?
Will Jimmy fall off his platform?
How to have a filled pattern
Are host configurations in the SSH config merged?
How to get a user’s choice with onPress props in React Native
What is React Native method of getting user’s input like React’s e.target.innerTextWhat is the difference between state and props in React?Hide keyboard in react-nativeWhat is the difference between using constructor vs getInitialState in React / React Native?How to pass props to this.props.childrenWhat is the difference between React Native and React?React Native child overflowing when inside parent with flex:1React Native - image pushes text out screen - fixed with 3rd view?Component won't stay wrapped within bounds (React Native)Not able to navigate to other page in react nativeHow to update styles on child component onPress in react native
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Good day everyone. please what is React Native’s replacement of React’s e.target.innerText? I’m trying to get what a user click on from TouchableOpacity component. I’m building a React Native quiz app.
I used TouchableOpacity for the options. I’m using onPress function to get the options the user clicks on with
<View style= flex: 2, flexDirection: 'row', justifyContent: 'space-between' >
<TouchableOpacity onPress = (e)=>this.props.onAnswer(e, currentQuestion)>
<View style= width: 171.5, height: 100, backgroundColor: 'lightgrey' >
<Text style=styles.options>questions[currentQuestion].options[0]</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress = (e)=>this.props.onAnswer(e, currentQuestion)>
<View style= width: 171.5, height: 100, backgroundColor: 'lightgrey' >
<Text style=styles.options>questions[currentQuestion].options[1]</Text>
</View>
</TouchableOpacity>
</View>
<View style= flex: 2.7, flexDirection: 'row', justifyContent: 'space-between' >
<TouchableOpacity onPress = (e)=>this.props.onAnswer(e, currentQuestion)>
<View style= width: 171.5, height: 100, backgroundColor: 'lightgrey' >
<Text style=styles.options>questions[currentQuestion].options[2]</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress = (e)=>this.props.onAnswer(e, currentQuestion)>
<View style= width: 171.5, height: 100, backgroundColor: 'lightgrey' >
<Text style=styles.options>
questions[currentQuestion].options[3]
</Text>
</View>
</TouchableOpacity>
</View>
So creating the answer function ... how do I determine if the user has chosen the correct answer.
onAnswer = () =>
return (
??? === questions[currentQuestion].answer ? this.onCorrect() : this.onWrong()
)
If it was React,I'd have replaced the '???' to e.target.innerText
Please help me out here.
reactjs react-native javascript-framework
add a comment |
Good day everyone. please what is React Native’s replacement of React’s e.target.innerText? I’m trying to get what a user click on from TouchableOpacity component. I’m building a React Native quiz app.
I used TouchableOpacity for the options. I’m using onPress function to get the options the user clicks on with
<View style= flex: 2, flexDirection: 'row', justifyContent: 'space-between' >
<TouchableOpacity onPress = (e)=>this.props.onAnswer(e, currentQuestion)>
<View style= width: 171.5, height: 100, backgroundColor: 'lightgrey' >
<Text style=styles.options>questions[currentQuestion].options[0]</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress = (e)=>this.props.onAnswer(e, currentQuestion)>
<View style= width: 171.5, height: 100, backgroundColor: 'lightgrey' >
<Text style=styles.options>questions[currentQuestion].options[1]</Text>
</View>
</TouchableOpacity>
</View>
<View style= flex: 2.7, flexDirection: 'row', justifyContent: 'space-between' >
<TouchableOpacity onPress = (e)=>this.props.onAnswer(e, currentQuestion)>
<View style= width: 171.5, height: 100, backgroundColor: 'lightgrey' >
<Text style=styles.options>questions[currentQuestion].options[2]</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress = (e)=>this.props.onAnswer(e, currentQuestion)>
<View style= width: 171.5, height: 100, backgroundColor: 'lightgrey' >
<Text style=styles.options>
questions[currentQuestion].options[3]
</Text>
</View>
</TouchableOpacity>
</View>
So creating the answer function ... how do I determine if the user has chosen the correct answer.
onAnswer = () =>
return (
??? === questions[currentQuestion].answer ? this.onCorrect() : this.onWrong()
)
If it was React,I'd have replaced the '???' to e.target.innerText
Please help me out here.
reactjs react-native javascript-framework
share your code please, in a readable format
– Alexandr Zavalii
Mar 25 at 21:35
add a comment |
Good day everyone. please what is React Native’s replacement of React’s e.target.innerText? I’m trying to get what a user click on from TouchableOpacity component. I’m building a React Native quiz app.
I used TouchableOpacity for the options. I’m using onPress function to get the options the user clicks on with
<View style= flex: 2, flexDirection: 'row', justifyContent: 'space-between' >
<TouchableOpacity onPress = (e)=>this.props.onAnswer(e, currentQuestion)>
<View style= width: 171.5, height: 100, backgroundColor: 'lightgrey' >
<Text style=styles.options>questions[currentQuestion].options[0]</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress = (e)=>this.props.onAnswer(e, currentQuestion)>
<View style= width: 171.5, height: 100, backgroundColor: 'lightgrey' >
<Text style=styles.options>questions[currentQuestion].options[1]</Text>
</View>
</TouchableOpacity>
</View>
<View style= flex: 2.7, flexDirection: 'row', justifyContent: 'space-between' >
<TouchableOpacity onPress = (e)=>this.props.onAnswer(e, currentQuestion)>
<View style= width: 171.5, height: 100, backgroundColor: 'lightgrey' >
<Text style=styles.options>questions[currentQuestion].options[2]</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress = (e)=>this.props.onAnswer(e, currentQuestion)>
<View style= width: 171.5, height: 100, backgroundColor: 'lightgrey' >
<Text style=styles.options>
questions[currentQuestion].options[3]
</Text>
</View>
</TouchableOpacity>
</View>
So creating the answer function ... how do I determine if the user has chosen the correct answer.
onAnswer = () =>
return (
??? === questions[currentQuestion].answer ? this.onCorrect() : this.onWrong()
)
If it was React,I'd have replaced the '???' to e.target.innerText
Please help me out here.
reactjs react-native javascript-framework
Good day everyone. please what is React Native’s replacement of React’s e.target.innerText? I’m trying to get what a user click on from TouchableOpacity component. I’m building a React Native quiz app.
I used TouchableOpacity for the options. I’m using onPress function to get the options the user clicks on with
<View style= flex: 2, flexDirection: 'row', justifyContent: 'space-between' >
<TouchableOpacity onPress = (e)=>this.props.onAnswer(e, currentQuestion)>
<View style= width: 171.5, height: 100, backgroundColor: 'lightgrey' >
<Text style=styles.options>questions[currentQuestion].options[0]</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress = (e)=>this.props.onAnswer(e, currentQuestion)>
<View style= width: 171.5, height: 100, backgroundColor: 'lightgrey' >
<Text style=styles.options>questions[currentQuestion].options[1]</Text>
</View>
</TouchableOpacity>
</View>
<View style= flex: 2.7, flexDirection: 'row', justifyContent: 'space-between' >
<TouchableOpacity onPress = (e)=>this.props.onAnswer(e, currentQuestion)>
<View style= width: 171.5, height: 100, backgroundColor: 'lightgrey' >
<Text style=styles.options>questions[currentQuestion].options[2]</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress = (e)=>this.props.onAnswer(e, currentQuestion)>
<View style= width: 171.5, height: 100, backgroundColor: 'lightgrey' >
<Text style=styles.options>
questions[currentQuestion].options[3]
</Text>
</View>
</TouchableOpacity>
</View>
So creating the answer function ... how do I determine if the user has chosen the correct answer.
onAnswer = () =>
return (
??? === questions[currentQuestion].answer ? this.onCorrect() : this.onWrong()
)
If it was React,I'd have replaced the '???' to e.target.innerText
Please help me out here.
reactjs react-native javascript-framework
reactjs react-native javascript-framework
edited Mar 25 at 22:10
Shadow_net
asked Mar 25 at 21:15
Shadow_netShadow_net
143 bronze badges
143 bronze badges
share your code please, in a readable format
– Alexandr Zavalii
Mar 25 at 21:35
add a comment |
share your code please, in a readable format
– Alexandr Zavalii
Mar 25 at 21:35
share your code please, in a readable format
– Alexandr Zavalii
Mar 25 at 21:35
share your code please, in a readable format
– Alexandr Zavalii
Mar 25 at 21:35
add a comment |
2 Answers
2
active
oldest
votes
EDIT:
My original answer below is similar to what you have. So just simply edit your onPress method like this:
onPress = (e)=>this.props.onAnswer(currentQuestion, questions[currentQuestion].options[0])
and your onAnswer will be able to take the users answer per questions:
onAnswer(question, usersAnswer)
..
render() ...
**
Mostly, react components should be driven by data or states and props (you don't want to work with your logic by relying to DOM elements with pure JS or JQuery).
For that, you want a react solution like below:
...
state =
data: [
id: 1,
name: 'Anna'
,
id: 2,
name: 'John'
,
id: 3,
name: 'James'
,
id: 4,
name: 'John'
]
onPersonPress(person)
console.log("e is ", person);
render()
const data = [] = this.state;
return (
<View>
data.map(person =>
return (<TouchableOpacity onPress=(e) => this.onPersonPress(person)>
<View><Text>Click Me - person.name</Text></View>
</TouchableOpacity>)
)
</View>
)
...
The reason why this is the most common way to deal with this kind of problem is because of it's flexibility and probably it's the most logical. With this, you can use the identifier (id) or any field instead of being forced to what Text displays 'innerText' which isn't always unique (in my case there's another John)
P.S: Others don't like this implementation because of performance considerations onPress=(e) => this.onPersonPress(person) so there are other ways to achieve the same.
What will the onAnswer function contains?
– Shadow_net
Mar 25 at 22:32
try logging variablesquestionandusersAnswer, those are the data you are interested in. Once you have them, you will know what your logic is on youronAnswerprobably your gonna send the user's answer to backend? or record the user's number of correct answers.
– Tyro Hunter
Mar 25 at 22:47
add a comment |
Try this:
import React from 'react';
import View, Text, TouchableOpacity from 'react-native';
const questions = [
question: 'question1 ?',
options: ['option1', 'option2', 'option3'],
answer: 'option2',
score: 10,
,
question: 'question2 ?',
options: ['option1', 'option2', 'option3'],
answer: 'option1',
score: 10,
,
question: 'question3 ?',
options: ['option1', 'option2', 'option3'],
answer: 'option3',
score: 10,
,
];
class QuizQuestion extends React.Component
constructor(props)
super(props);
this.state =
totalScore: 0,
answered: questions.map(() => false), // All questions' answered state = false at statup
;
render()
return (
<View
style=
flex: 2,
flexDirection: 'row',
justifyContent: 'space-between',
>
questions.map((
question, options, answer, score,
, index) => (
<View>
<View>
<Text>question</Text>
</View>
/* Options */
<View>
options.map(( option ) => (
<TouchableOpacity
onPress=() =>
let totalScore, answered = this.state;
if (!answered[index] && option === answer)
answered[index] = true;
this.setState(
totalScore: totalScore + score,
answered,
);
>
<View>
<Text>option</Text>
</View>
</TouchableOpacity>
))
</View>
</View>
))
</View>
);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55346511%2fhow-to-get-a-user-s-choice-with-onpress-props-in-react-native%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
EDIT:
My original answer below is similar to what you have. So just simply edit your onPress method like this:
onPress = (e)=>this.props.onAnswer(currentQuestion, questions[currentQuestion].options[0])
and your onAnswer will be able to take the users answer per questions:
onAnswer(question, usersAnswer)
..
render() ...
**
Mostly, react components should be driven by data or states and props (you don't want to work with your logic by relying to DOM elements with pure JS or JQuery).
For that, you want a react solution like below:
...
state =
data: [
id: 1,
name: 'Anna'
,
id: 2,
name: 'John'
,
id: 3,
name: 'James'
,
id: 4,
name: 'John'
]
onPersonPress(person)
console.log("e is ", person);
render()
const data = [] = this.state;
return (
<View>
data.map(person =>
return (<TouchableOpacity onPress=(e) => this.onPersonPress(person)>
<View><Text>Click Me - person.name</Text></View>
</TouchableOpacity>)
)
</View>
)
...
The reason why this is the most common way to deal with this kind of problem is because of it's flexibility and probably it's the most logical. With this, you can use the identifier (id) or any field instead of being forced to what Text displays 'innerText' which isn't always unique (in my case there's another John)
P.S: Others don't like this implementation because of performance considerations onPress=(e) => this.onPersonPress(person) so there are other ways to achieve the same.
What will the onAnswer function contains?
– Shadow_net
Mar 25 at 22:32
try logging variablesquestionandusersAnswer, those are the data you are interested in. Once you have them, you will know what your logic is on youronAnswerprobably your gonna send the user's answer to backend? or record the user's number of correct answers.
– Tyro Hunter
Mar 25 at 22:47
add a comment |
EDIT:
My original answer below is similar to what you have. So just simply edit your onPress method like this:
onPress = (e)=>this.props.onAnswer(currentQuestion, questions[currentQuestion].options[0])
and your onAnswer will be able to take the users answer per questions:
onAnswer(question, usersAnswer)
..
render() ...
**
Mostly, react components should be driven by data or states and props (you don't want to work with your logic by relying to DOM elements with pure JS or JQuery).
For that, you want a react solution like below:
...
state =
data: [
id: 1,
name: 'Anna'
,
id: 2,
name: 'John'
,
id: 3,
name: 'James'
,
id: 4,
name: 'John'
]
onPersonPress(person)
console.log("e is ", person);
render()
const data = [] = this.state;
return (
<View>
data.map(person =>
return (<TouchableOpacity onPress=(e) => this.onPersonPress(person)>
<View><Text>Click Me - person.name</Text></View>
</TouchableOpacity>)
)
</View>
)
...
The reason why this is the most common way to deal with this kind of problem is because of it's flexibility and probably it's the most logical. With this, you can use the identifier (id) or any field instead of being forced to what Text displays 'innerText' which isn't always unique (in my case there's another John)
P.S: Others don't like this implementation because of performance considerations onPress=(e) => this.onPersonPress(person) so there are other ways to achieve the same.
What will the onAnswer function contains?
– Shadow_net
Mar 25 at 22:32
try logging variablesquestionandusersAnswer, those are the data you are interested in. Once you have them, you will know what your logic is on youronAnswerprobably your gonna send the user's answer to backend? or record the user's number of correct answers.
– Tyro Hunter
Mar 25 at 22:47
add a comment |
EDIT:
My original answer below is similar to what you have. So just simply edit your onPress method like this:
onPress = (e)=>this.props.onAnswer(currentQuestion, questions[currentQuestion].options[0])
and your onAnswer will be able to take the users answer per questions:
onAnswer(question, usersAnswer)
..
render() ...
**
Mostly, react components should be driven by data or states and props (you don't want to work with your logic by relying to DOM elements with pure JS or JQuery).
For that, you want a react solution like below:
...
state =
data: [
id: 1,
name: 'Anna'
,
id: 2,
name: 'John'
,
id: 3,
name: 'James'
,
id: 4,
name: 'John'
]
onPersonPress(person)
console.log("e is ", person);
render()
const data = [] = this.state;
return (
<View>
data.map(person =>
return (<TouchableOpacity onPress=(e) => this.onPersonPress(person)>
<View><Text>Click Me - person.name</Text></View>
</TouchableOpacity>)
)
</View>
)
...
The reason why this is the most common way to deal with this kind of problem is because of it's flexibility and probably it's the most logical. With this, you can use the identifier (id) or any field instead of being forced to what Text displays 'innerText' which isn't always unique (in my case there's another John)
P.S: Others don't like this implementation because of performance considerations onPress=(e) => this.onPersonPress(person) so there are other ways to achieve the same.
EDIT:
My original answer below is similar to what you have. So just simply edit your onPress method like this:
onPress = (e)=>this.props.onAnswer(currentQuestion, questions[currentQuestion].options[0])
and your onAnswer will be able to take the users answer per questions:
onAnswer(question, usersAnswer)
..
render() ...
**
Mostly, react components should be driven by data or states and props (you don't want to work with your logic by relying to DOM elements with pure JS or JQuery).
For that, you want a react solution like below:
...
state =
data: [
id: 1,
name: 'Anna'
,
id: 2,
name: 'John'
,
id: 3,
name: 'James'
,
id: 4,
name: 'John'
]
onPersonPress(person)
console.log("e is ", person);
render()
const data = [] = this.state;
return (
<View>
data.map(person =>
return (<TouchableOpacity onPress=(e) => this.onPersonPress(person)>
<View><Text>Click Me - person.name</Text></View>
</TouchableOpacity>)
)
</View>
)
...
The reason why this is the most common way to deal with this kind of problem is because of it's flexibility and probably it's the most logical. With this, you can use the identifier (id) or any field instead of being forced to what Text displays 'innerText' which isn't always unique (in my case there's another John)
P.S: Others don't like this implementation because of performance considerations onPress=(e) => this.onPersonPress(person) so there are other ways to achieve the same.
edited Mar 25 at 22:25
answered Mar 25 at 22:18
Tyro HunterTyro Hunter
5197 silver badges19 bronze badges
5197 silver badges19 bronze badges
What will the onAnswer function contains?
– Shadow_net
Mar 25 at 22:32
try logging variablesquestionandusersAnswer, those are the data you are interested in. Once you have them, you will know what your logic is on youronAnswerprobably your gonna send the user's answer to backend? or record the user's number of correct answers.
– Tyro Hunter
Mar 25 at 22:47
add a comment |
What will the onAnswer function contains?
– Shadow_net
Mar 25 at 22:32
try logging variablesquestionandusersAnswer, those are the data you are interested in. Once you have them, you will know what your logic is on youronAnswerprobably your gonna send the user's answer to backend? or record the user's number of correct answers.
– Tyro Hunter
Mar 25 at 22:47
What will the onAnswer function contains?
– Shadow_net
Mar 25 at 22:32
What will the onAnswer function contains?
– Shadow_net
Mar 25 at 22:32
try logging variables
question and usersAnswer, those are the data you are interested in. Once you have them, you will know what your logic is on your onAnswer probably your gonna send the user's answer to backend? or record the user's number of correct answers.– Tyro Hunter
Mar 25 at 22:47
try logging variables
question and usersAnswer, those are the data you are interested in. Once you have them, you will know what your logic is on your onAnswer probably your gonna send the user's answer to backend? or record the user's number of correct answers.– Tyro Hunter
Mar 25 at 22:47
add a comment |
Try this:
import React from 'react';
import View, Text, TouchableOpacity from 'react-native';
const questions = [
question: 'question1 ?',
options: ['option1', 'option2', 'option3'],
answer: 'option2',
score: 10,
,
question: 'question2 ?',
options: ['option1', 'option2', 'option3'],
answer: 'option1',
score: 10,
,
question: 'question3 ?',
options: ['option1', 'option2', 'option3'],
answer: 'option3',
score: 10,
,
];
class QuizQuestion extends React.Component
constructor(props)
super(props);
this.state =
totalScore: 0,
answered: questions.map(() => false), // All questions' answered state = false at statup
;
render()
return (
<View
style=
flex: 2,
flexDirection: 'row',
justifyContent: 'space-between',
>
questions.map((
question, options, answer, score,
, index) => (
<View>
<View>
<Text>question</Text>
</View>
/* Options */
<View>
options.map(( option ) => (
<TouchableOpacity
onPress=() =>
let totalScore, answered = this.state;
if (!answered[index] && option === answer)
answered[index] = true;
this.setState(
totalScore: totalScore + score,
answered,
);
>
<View>
<Text>option</Text>
</View>
</TouchableOpacity>
))
</View>
</View>
))
</View>
);
add a comment |
Try this:
import React from 'react';
import View, Text, TouchableOpacity from 'react-native';
const questions = [
question: 'question1 ?',
options: ['option1', 'option2', 'option3'],
answer: 'option2',
score: 10,
,
question: 'question2 ?',
options: ['option1', 'option2', 'option3'],
answer: 'option1',
score: 10,
,
question: 'question3 ?',
options: ['option1', 'option2', 'option3'],
answer: 'option3',
score: 10,
,
];
class QuizQuestion extends React.Component
constructor(props)
super(props);
this.state =
totalScore: 0,
answered: questions.map(() => false), // All questions' answered state = false at statup
;
render()
return (
<View
style=
flex: 2,
flexDirection: 'row',
justifyContent: 'space-between',
>
questions.map((
question, options, answer, score,
, index) => (
<View>
<View>
<Text>question</Text>
</View>
/* Options */
<View>
options.map(( option ) => (
<TouchableOpacity
onPress=() =>
let totalScore, answered = this.state;
if (!answered[index] && option === answer)
answered[index] = true;
this.setState(
totalScore: totalScore + score,
answered,
);
>
<View>
<Text>option</Text>
</View>
</TouchableOpacity>
))
</View>
</View>
))
</View>
);
add a comment |
Try this:
import React from 'react';
import View, Text, TouchableOpacity from 'react-native';
const questions = [
question: 'question1 ?',
options: ['option1', 'option2', 'option3'],
answer: 'option2',
score: 10,
,
question: 'question2 ?',
options: ['option1', 'option2', 'option3'],
answer: 'option1',
score: 10,
,
question: 'question3 ?',
options: ['option1', 'option2', 'option3'],
answer: 'option3',
score: 10,
,
];
class QuizQuestion extends React.Component
constructor(props)
super(props);
this.state =
totalScore: 0,
answered: questions.map(() => false), // All questions' answered state = false at statup
;
render()
return (
<View
style=
flex: 2,
flexDirection: 'row',
justifyContent: 'space-between',
>
questions.map((
question, options, answer, score,
, index) => (
<View>
<View>
<Text>question</Text>
</View>
/* Options */
<View>
options.map(( option ) => (
<TouchableOpacity
onPress=() =>
let totalScore, answered = this.state;
if (!answered[index] && option === answer)
answered[index] = true;
this.setState(
totalScore: totalScore + score,
answered,
);
>
<View>
<Text>option</Text>
</View>
</TouchableOpacity>
))
</View>
</View>
))
</View>
);
Try this:
import React from 'react';
import View, Text, TouchableOpacity from 'react-native';
const questions = [
question: 'question1 ?',
options: ['option1', 'option2', 'option3'],
answer: 'option2',
score: 10,
,
question: 'question2 ?',
options: ['option1', 'option2', 'option3'],
answer: 'option1',
score: 10,
,
question: 'question3 ?',
options: ['option1', 'option2', 'option3'],
answer: 'option3',
score: 10,
,
];
class QuizQuestion extends React.Component
constructor(props)
super(props);
this.state =
totalScore: 0,
answered: questions.map(() => false), // All questions' answered state = false at statup
;
render()
return (
<View
style=
flex: 2,
flexDirection: 'row',
justifyContent: 'space-between',
>
questions.map((
question, options, answer, score,
, index) => (
<View>
<View>
<Text>question</Text>
</View>
/* Options */
<View>
options.map(( option ) => (
<TouchableOpacity
onPress=() =>
let totalScore, answered = this.state;
if (!answered[index] && option === answer)
answered[index] = true;
this.setState(
totalScore: totalScore + score,
answered,
);
>
<View>
<Text>option</Text>
</View>
</TouchableOpacity>
))
</View>
</View>
))
</View>
);
edited Mar 26 at 5:07
answered Mar 26 at 4:57
Hend El-SahliHend El-Sahli
1,4651 gold badge3 silver badges16 bronze badges
1,4651 gold badge3 silver badges16 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55346511%2fhow-to-get-a-user-s-choice-with-onpress-props-in-react-native%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
share your code please, in a readable format
– Alexandr Zavalii
Mar 25 at 21:35