# Fundamental React question

**URL:** <https://forum.freecodecamp.org/t/fundamental-react-question/355885>\
**Category:** General\
**Created:** [March 4, 2020, 10:42pm UTC](https://forum.freecodecamp.org/t/fundamental-react-question/355885 "2020-03-04T22:42:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sabbyiqbal](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sabbyiqbal/32/104864_2.png) [@sabbyiqbal](https://forum.freecodecamp.org/u/sabbyiqbal)\
**Post date:** [March 4, 2020, 10:42pm UTC](https://forum.freecodecamp.org/t/fundamental-react-question/355885/1 "2020-03-04T22:42:03Z")

</div>

Hi , I know I am just forgetting something fundamental about React here but I’ve been circling this problem for too long now:

I want to print the data that is returned from `generateRandomCard()` but when I click on button, nothing shows up. A couple of `console.log`s in `generateRandomCard()` showed correct values so that’s OK.

Code:

```auto
import React, { Component } from "react";

class FlashCard extends Component {

  state = {

    data: [

     //a bunch of arrays

    ]

  };

  generateRandomCard = () => {

    let randomNumber = Math.floor(Math.random() * Math.floor(9));

    let randomCard = this.state.data[randomNumber];

  };

  render() {

    console.log(this.randomCard);

    return (

      <React.Fragment>

        <div id="selections" className="row">

          <div className="col-md-6">

            <select className="form-control form-control-sm">

              <option value="makeChoice">

                I want to test my knowledge of....

              </option>

              <option value="urduFront">Nastaleeq</option>

              <option value="urduFront">Romanized</option>

              <option value="urduFront">Translation</option>

            </select>

          </div>

        </div>

        <div className="card">

          <div className="card-body">

            <h6>{this.randomCard}</h6>

          </div>

        </div>

        <button onClick={this.generateRandomCard}>Generate New Card</button>

      </React.Fragment>

    );

  }

}

export default FlashCard;

```

---

<div class="post-metadata">

**Author:** ![biscuitmanz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/biscuitmanz/32/188814_2.png) [@biscuitmanz](https://forum.freecodecamp.org/u/biscuitmanz)\
**Post date:** [March 4, 2020, 11:01pm UTC](https://forum.freecodecamp.org/t/fundamental-react-question/355885/2 "2020-03-04T23:01:51Z")

</div>

> [@sabbyiqbal](#):
>
> let randomCard = this.state.data[randomNumber];

@sabbyiqbal  
you cant access this because it is inside a function, you should have randomCard as state instead and the use setState to add your random card to it, only state will re render the page

---

<div class="post-metadata">

**Author:** ![Marmiz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marmiz/32/74845_2.png) [@Marmiz](https://forum.freecodecamp.org/u/Marmiz)\
**Post date:** [March 5, 2020, 2:36am UTC](https://forum.freecodecamp.org/t/fundamental-react-question/355885/3 "2020-03-05T02:36:18Z")

</div>

For reference have a look at React API documentation about updating:

> **[React.Component – React](https://legacy.reactjs.org/docs/react-component.html)**
>
> A JavaScript library for building user interfaces

> An update can be caused by changes to props or state.

So `this.randomCard` needs to be in some sort of state or prop if you want React to reflect its updating values.  
Hope this helps 👍

---

<div class="post-metadata">

**Author:** ![padunk](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/padunk/32/71781_2.png) [@padunk](https://forum.freecodecamp.org/u/padunk)\
**Post date:** [March 5, 2020, 3:28am UTC](https://forum.freecodecamp.org/t/fundamental-react-question/355885/4 "2020-03-05T03:28:40Z")

</div>

The method `generateRandomCard` doesn’t do anything.  
It just storing the value into a variable.  
If you want to access `randomCard` correctly, then as @biscuitmanz said, put it on the state, then when you called `getRandomCard()` use `setState` to change the value.  
One more thing, for `randomNumber` you can do:  
`let randomNumber = Math.floor(Math.random() * 9)` or  
`let randomNumber = Math.floor(Math.random() * this.state.data.length)`

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 7:34pm UTC](https://forum.freecodecamp.org/t/fundamental-react-question/355885/5 "2021-06-01T19:34:39Z")

</div>

Closed on June 1, 2021, 7:34pm UTC


