Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

84
Views
Dynamic prop value in react?

I want a dynamic prop value for react. I already have a list of Fruits, but my prop value should be Fruits[0] + 'prop'

For example: ApplesProp

index.js

const ApplesProp = { Name: "Green", Age: 34 }

const Fruits = ["Apples", "Pears", "Oranges"]

<App prop={dynamic-fruit+'Prop'} />

Tried

<App prop={Fruits[0]+'Prop'}/>

but this results in passing the string: 'ApplesProp' not the ApplesProp object (i.e. Name: Green, Age: 34).

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Fruits[0]+'Prop'

Adding a string to a string returns a string.

To accomplish what you need, you can create an object with fruits as keys and props as values:

const fruitVsProps = {
 Apples: ApplesProp
 // add more as you like
}

<App prop={fruitVsProps[Fruits[0]]} />
about 4 years ago · Juan Pablo Isaza Report

0

there is two way you can Do it. 1.better way use array.map() then pass to your props.

 let Fruits = ["Apples", "Pears", "Oranges"];
<ff> {Fruits.map((item)=>{<App props={item} /> })} </ff>

2.second way you can use literal template

about 4 years ago · Juan Pablo Isaza Report

0

Create an object and add your object in that

const props = {
    ApplesProp: { Name: "Green", Age: 34 }
  }

const Fruits = ["Apples", "Pears", "Oranges"]

console.log(props[Fruits[0]+'Prop'])

and now you could use it like

<App prop={props[Fruits[0]+'Prop']} />
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!