2

I have a reducer array of Q/A objects. I have them set to two different divs. I am needing my "Next" button to trigger a function that will generate a random number that will determine the next object to be displayed from the array. The "Next" button is successfully triggering the function, and the function is successfully creating a random number, but the question and answer props are not changing based on the random number. How do I generate a random number and use that number to choose an object from an array?

Here is my code GitHub

2
  • 1
    React listen changes on state variable, not all variables. You need to setState and use this.state.randomNumber instead of this.randomNumber. Commented Jul 3, 2020 at 6:39
  • Another problem is using direct dom changes. Don't do that when you are using React. Commented Jul 3, 2020 at 6:44

2 Answers 2

6

First of all I would get rid of the class structure, use a functional component instead. Second, get rid of the setState etc. use the newer React hooks: https://reactjs.org/docs/hooks-state.html

Your code readability will improve greatly.

import React, {useState} from "react";

const questionAnswer: [
    {question: "Question 1", answer: "Answer 1"},
    {question: "Question 2", answer: "Answer 2"},
    {question: "Question 3", answer: "Answer 3"},
    {question: "Question 4", answer: "Answer 4"}
];

export const FlashCards = ({questionAnswer}) => {
    
    const [randomNumber, setRandomNumber] = useState(0)
    
    const flipDisplay = () => {
        // Logic flip display here.
    }
    
    const generateRandomNumber = () => {
        const randomNumber = Math.floor(Math.random() * questionAnswer.length);
        setRandomNumber(randomNumber)
    }

    return (
        <div className='ui container'>
            <div id='question'>
                {questionAnswer[randomNumber].question}
            </div>
            <div id='answer' style={{display: 'none'}}>
                {questionAnswer[randomNumber].answer}
            </div>
            <div>
                <button className='ui button' onClick={flipDisplay}>Flip</button>
                <button className='ui button' onClick={generateRandomNumber}>Next</button>
            </div>
        </div>
    )
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help, I appreciate it.
1

Declare a state randomNumber and update that using setState. React would re-render when your state updates.

Try this

import React from 'react';
import { connect } from 'react-redux';


class FlashCards extends React.Component {

    state = {
       randomNumber: 0
    }

    randomQuestion = (arry=[]) => {
        var num = Math.floor(Math.random() * arry.length);
        //const display = arry[Math.floor(Math.random() * arry.length)];
        this.setState({randomNumber: num});
    }


    flipDisplay = () => {
        // your flipDisplay function, no changes here
    }


    render() {
        return(
            <div className='ui container'>
                <div id='question'>
                { this.props.questionAnswer[this.state.randomNumber].question }
                </div>
                <div id='answer' style={{ display: 'none' }}>
                { this.props.questionAnswer[this.state.randomNumber].answer }
                </div>
                
                <div>
                    <button className='ui button' onClick={this.flipDisplay}>Flip</button>
                    <button className='ui button' onClick={() => this.randomQuestion(this.props.questionAnswer)}>Next</button>
                </div>
            </div>
        );
    }
}




const mapStateToProps = (state) => {
    return {
        questionAnswer: state.questionAnswer
    };
}

export default connect(mapStateToProps)(FlashCards);

1 Comment

Thank you for the suggestion!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.