React with Typescript: Render components from `map` function

colin_dev256

I have below data that I get from a json file and also import a utility function that picks out 5 random entities from an object Array in this case data array which has 30 entities.

I'm struggling to render the 5 random jockies that are stored in unique. I'm fairly new to the ES6 syntax. How can I render the 5 jockeys from unique? Currently I'm getting

TypeError: Cannot read property 'avatar_url' of undefined

import * as React from 'react';
import { Jockey } from '../Jockey';
const data: JockeyArray[] = require('../../team.json');
import { getUniqueJockeys } from '../../utils/uniqueUtil';

interface JockeyArray {
    id: number;
    login: string;
    avatar_url: string;
}

interface State {
    nextRacers: string[];
    jockeys: JockeyArray[];
}

export class Race extends React.Component<Props, State> { 
    constructor(props: Props) {
        super(props);
        this.state = {
            jockeys: data as JockeyArray[], 
            nextRacers: [],
        };
    }

    render() {
        
        if (this.props.startRandom) { // random selection
            
            // returns data array as 5 unique entities
            const unique = getUniqueJockeys(data);
            console.log(unique);//I can see my 5 randoms generated.
        
        return (
          <div>
            { unique.map((racer, index) => (       
                        // ??
                   <Jockey avatar={racer[index].avatar_url} />
              ))}
          </div>
        )
    }

}

Definition of getUniqueJockeys

// Selecting 5 unique random jockeys
export const getUniqueJockeys = (anyArray: Array<object>) => {
    let uniqueArray = [];
    while (uniqueArray.length < 5) {
        const unique = Math.floor(Math.random() * anyArray.length);
        if (uniqueArray.indexOf(anyArray[unique]) > -1) {
            continue;
        }
        uniqueArray.push(anyArray[unique]);
    }
    return uniqueArray;
};

Titian Cernicova-Dragomir

You should change the definition of getUniqueJockeys to be more generic. If data is of type JockeyArray[] this should work (and you can use it for any values not just JockeyArray):

export const getUniqueJockeys = <T>(anyArray: Array<T>) => {
    let uniqueArray: T[] = [];
    while (uniqueArray.length < 5) {
        const unique = Math.floor(Math.random() * anyArray.length);
        if (uniqueArray.indexOf(anyArray[unique]) > -1) {
            continue;
        }
        uniqueArray.push(anyArray[unique]);
    }
    return uniqueArray;
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

how to render react components by using map and join

From Java

Rendering React components with promises inside the render method

From Java

save image from React map function

From Dev

React waiting for map function before deciding render to prevent div flash

From Dev

React render array returned from map

From Dev

Generate React components in PHP and then render in client

From Dev

How to dynamically load & render react components?

From Dev

Is there a way to render multiple React components in the React.render() function?

From Dev

Extending React components in TypeScript

From Dev

Call external Javascript function from react components

From Dev

React call component's method from nested function in render method

From Dev

React render components from string

From Dev

Render multiple components in React Router

From Dev

Render Content Dynamically from an array map function in React Native

From Dev

Generic React components in TypeScript/JSX?

From Dev

Spread an array of components in render in React

From Dev

How to render a modal with map() from arraylist in react

From Dev

map.get is not a function while retrieving a value from a map in typescript

From Dev

React waiting for map function before deciding render to prevent div flash

From Dev

React - call handler from var in render function

From Dev

react call a function from ReactDOM.render

From Dev

How to progressively render react components?

From Dev

Pass the value of a props from a function to render() in React.JS

From Dev

Get value from firebase and render it to render function in react

From Dev

React with Typescript: Types for array.map() with arrow function

From Dev

Pass function from props to render function in react

From Dev

render state in map function and add some style react js

From Dev

How to render component from a function outside the render method in React?

From Dev

How to render components using function in React Native

Related Related

  1. 1

    how to render react components by using map and join

  2. 2

    Rendering React components with promises inside the render method

  3. 3

    save image from React map function

  4. 4

    React waiting for map function before deciding render to prevent div flash

  5. 5

    React render array returned from map

  6. 6

    Generate React components in PHP and then render in client

  7. 7

    How to dynamically load & render react components?

  8. 8

    Is there a way to render multiple React components in the React.render() function?

  9. 9

    Extending React components in TypeScript

  10. 10

    Call external Javascript function from react components

  11. 11

    React call component's method from nested function in render method

  12. 12

    React render components from string

  13. 13

    Render multiple components in React Router

  14. 14

    Render Content Dynamically from an array map function in React Native

  15. 15

    Generic React components in TypeScript/JSX?

  16. 16

    Spread an array of components in render in React

  17. 17

    How to render a modal with map() from arraylist in react

  18. 18

    map.get is not a function while retrieving a value from a map in typescript

  19. 19

    React waiting for map function before deciding render to prevent div flash

  20. 20

    React - call handler from var in render function

  21. 21

    react call a function from ReactDOM.render

  22. 22

    How to progressively render react components?

  23. 23

    Pass the value of a props from a function to render() in React.JS

  24. 24

    Get value from firebase and render it to render function in react

  25. 25

    React with Typescript: Types for array.map() with arrow function

  26. 26

    Pass function from props to render function in react

  27. 27

    render state in map function and add some style react js

  28. 28

    How to render component from a function outside the render method in React?

  29. 29

    How to render components using function in React Native

HotTag

Archive