0

I am not able to render any elements returned from a function in rendering

this.state.globalConfirmedCases.isLoading 
                        ? 
                    <Loading/> 
                        : 
                    this.generateCount(this.state.globalConfirmedCases)
generateCount = ()=> {
        this.state.globalConfirmedCases.data.map((item, index)=>{
            console.log(typeof(item.confirmed.value))
            console.log(this.state.globalConfirmedCases.data)
            return (
                <CountUp
                    key={index}
                    end={item.confirmed.value}
                />
            )
        })
    }

What I am doing wrong here?

1
  • I am wondering if its race condition. meaning your state might not have data when it renders? do you see output in console.log(this.state.globalConfirmedCases.data) ? Commented May 5, 2020 at 19:47

2 Answers 2

2

Your generateCount() function does not return anything, just undefined.

Try instead as the following in order to return what .map() creates:

generateCount = () => this.state.globalConfirmedCases.data.map((item, index) => 
   <CountUp key={index} end={item.confirmed.value} />
)

See an example for difference:

const array = [1,2,3];

const example1 = () => {
   array.map(e => e);
}

const example2 = () => array.map(e => e);
    
console.log(example1());
console.log(example2());

I hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

@JithinZacharia Happy to help!
0

You should share more detailed code here for better understanding the issue. Anyhow, what I think is you are not returning the function. Write return in front of this keyword like:

generateCount = ()=> {
        return this.state.globalConfirmedCases.data.map((item, index)=>{
            console.log(typeof(item.confirmed.value))
            console.log(this.state.globalConfirmedCases.data)
            return (
                <CountUp
                    key={index}
                    end={item.confirmed.value}
                />
            )
        })
    }

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.