0

I am trying to use a variable when I fetch a array of code to create my menu with the IDs.

Here is the beginning of my function

// Constants import - fixtures
import {dummyData, trendingData, COLORS, FONTS} from '../constants';


export default function ScrollXBar({
  secondary,
  setSecondary,
  COUNTRIES_ITEM_SIZE,
  primary,
  mainScrollX,
  database,

Here is where I create the Menu

        // Set Place
        setSecondary([
          {id: -1},
          ...dummyData.primary[position].secondary,
          {id: -2},
        ]);
      }}

I would like to remplace "dummyData" object by the variable "database" which is the name of another depending on what was passed in the component.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Yes, like "...database" I am having an error when I write the code like this. If this is the correct way to call the variable, the array that I pass in database that may be wrong? – Canadian Lonman May 22 '21 at 15:32
  • dummy data: ``` export const primary = [ { id: 1, title: 'California', image: require('../assets/images/california/LosAngeles.png'), secondary: [ { id: 1, subtitle: 'Chase', ``` ... const dummyData = {primary}; export default dummyData; – Canadian Lonman May 22 '21 at 16:03
  • if I use ...database.primary. I get with React Native ``` undefined is not an object (evaluating 'database.primary[position]') ``` – Canadian Lonman May 22 '21 at 16:07
  • export const primary = [ { id: 1, title: 'All', image: 'road', secondary: [ { id: 1, subtitle: 'U-Haul Chaser', ... const trendingData = {primary}; export default trendingData; – Canadian Lonman May 22 '21 at 16:17
  • I passe trendingData in database Then I import all the array with import {dummyData, trendingData, COLORS, FONTS} from '../constants'; I log database and I get trendingData LOG trendingData ERROR TypeError: undefined is not an object (evaluating 'database.primary[position]') – Canadian Lonman May 22 '21 at 16:18
  • I did a sandbox https://codesandbox.io/s/compassionate-cloud-3wy2m?file=/src/components/ScrollXBar.js:1087-1138 Line 42, I would like to have it dynamic to select the correct object in the constants folder – Canadian Lonman May 22 '21 at 17:55
  • Something like this `database === "trendingData" ? [...trendingData.primary[position].secondary] : [...dummyData.primary[position].secondary],`? – Ajeet Shah May 22 '21 at 18:31
  • Your code would work. I was wondering if there was another way to do it without a if statement. – Canadian Lonman May 22 '21 at 18:56
  • Well. I am not sure I just do an app to learn React Native. From the top of my head. At one point it may be like Top5, Trending, Lower price etc – Canadian Lonman May 22 '21 at 19:42
  • See [this sandbox](https://codesandbox.io/s/so-dynamic-property-mp78f) (it doesn't use `if-else`, it uses dynamic object property). And read about [Dynamically access object property using variable](https://stackoverflow.com/q/4244896/2873538). – Ajeet Shah May 22 '21 at 20:11
  • 1
    Totally work. Thank you! You honestly saved me – Canadian Lonman May 22 '21 at 22:02

0 Answers0