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

103
Views
Set Prop Key Dynamically JSX

I'm wondering if it's possible to do the following in JSX (this code does not work, just an example of what I'm trying to do):

const propKey = 'someProp';
const jsx = <MyComponent [propKey]="some value" />;
// evaluates to <MyComponent someProp="some value">

I'm using babel es2015 stage-1, so I could spread a dictionary like this:

const extraProps = {[propKey]: 'some value'};
const jsx = <MyComponent {...extraProps} />

but that seems too clunky for just one prop. I'm not looking to use React.createElement; all my classes are es6 classes. Thanks!

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can create in the JSX an object using the computed property names, and spread it:

const propKey = 'someProp';
const jsx = <MyComponent { ...{ [propKey]: "some value" } } />;
over 4 years ago · Santiago Trujillo Report

0

JSX is syntactic sugar for calling React.createElement. So one solution is to call React.createElement directly:

const element = React.createElement(MyComponent, {[propKey]: 'some value'});
over 4 years ago · Santiago Trujillo 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!