1

After calling setTimeOut(Question, 3000) I receive the error "Cannot call a class as a function". "Question" needs to be a class in order to get the lifecycle methods that classes receive.

The only answer I have found so far is to include "extends React.Component" on the class, which I have.

import React from 'react';
import Question from './Question';

setTimeout(Question, 3000);


function Congratulations(props) {
    return(
        <div>
        <h1>Congraulations you are the champion.</h1>
        <h2>Your Score: {props.score}</h2>
    </div>
    )
}  
export default Congratulations;

The first few lines of Question component class follows.

export default class Question extends React.Component {
constructor(props) {
    super(props);

I would like for Question to appear on the DOM after 3 seconds.

2 Answers 2

2

you can use in componentdidmount method and set state for show after 3 seconds. In render method, you can render only if show is true.

In the constructor method, create a state with show = false.

In componentDidMount yo can create

setTimeOut(() => this.setState({ show: true}), 3000)

In render method you can render with that state

{
 show &&
  // render what you want
}
Sign up to request clarification or add additional context in comments.

2 Comments

This worked perfectly. For future viewers, I would edit your post to setTimeout... Thanks, Fernando! I do have a follow-up question. Do you know what the, I assume to be, conditional statement in your render is called? I have not seen that syntax, and I would like to do further reading. (I am obviously new to this.)
If you refer to &&, that the operator tu eval the first arg (this case show) and if this is true, returns or render the sentence after &&. Also you have || that means 'or'. you can use with variable to eval the first arg too. const test = true || 'string' Its mean, if the first arg is true, test is equal to true. If not, is equal to 'string'.
0

The timeout isn't your problem, you just don't have Reacts setup code. Check out React-Dom's render method. You need to wrap the setup code in a function and then trigger that function with setTimeout. Even a React function component isn't going to do much if you call it directly, like what setTimeout would do.

Comments

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.