Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to have variable attributes in JSX?

Tags:

reactjs

jsx

Hopefully my question is clear, I'm mainly looking for a way to dynamically attach attributes to a JSX input.

<input type="text" {variableAttribute}={anotherVariable} />

Is something like this possible without overriding the way JSX compiles from JS to regular HTML?

like image 550
Chris Hawkes Avatar asked Jul 27 '16 17:07

Chris Hawkes


People also ask

How do you declare a variable in JSX?

Embedding Expressions in JSX In the example below, we declare a variable called name and then use it inside JSX by wrapping it in curly braces: const name = 'Josh Perez';const element = <h1>Hello, {name}</h1>; You can put any valid JavaScript expression inside the curly braces in JSX. For example, 2 + 2 , user.

Can you put JSX in a variable?

JSX stands for JavaScript XML, a coding standard that allows you to use JavaScript expressions and other HTML features inline. Using JSX, you can create a function and return a set of JSX elements to a variable, and that variable used is to render the elements inside the render() function in React.

What should be the type attribute of the file which contains JSX code?

The class attribute is a much used attribute in HTML, but since JSX is rendered as JavaScript, and the class keyword is a reserved word in JavaScript, you are not allowed to use it in JSX. Use attribute className instead. JSX solved this by using className instead.

How do you pass data attributes in React?

To set a data attribute on an element in React, set the attribute directly on the element, e.g. <button data-test-id="my-btn"> or use the setAttribute() method, e.g. el. setAttribute('data-foo', 'bar') . You can access the element on the event object or using a ref .

How do you specify attributes in JSX?

Specifying Attributes with JSX. You may use quotes to specify string literals as attributes: const element = <div tabIndex="0"></div>; You may also use curly braces to embed a JavaScript expression in an attribute: const element = <img src={user.avatarUrl}></img>;

How to use variables in JavaScript?

In programming, just like in algebra, we use variables in expressions (total = price1 + price2). From the example above, you can calculate the total to be 11. JavaScript variables are containers for storing data values. All JavaScript variables must be identified with unique names. These unique names are called identifiers.

What is JSX in JavaScript?

JSX is an Expression Too. After compilation, JSX expressions become regular JavaScript function calls and evaluate to JavaScript objects. This means that you can use JSX inside of if statements and for loops, assign it to variables, accept it as arguments, and return it from functions:

Can you use class in JSX?

Attribute class = className The class attribute is a much used attribute in HTML, but since JSX is rendered as JavaScript, and the class keyword is a reserved word in JavaScript, you are not allowed to use it in JSX. Use attribute className instead. JSX solved this by using className instead.


1 Answers

You can initialize an object with a computed property name, and then use JSX Spread Attributes to convert it to attribute:

const DemoComponent = ({ variablePropName, variablePropValue }) => { 
    const variableAttribute = { [variablePropName]: variablePropValue };
    return (
        <input type="text" { ...variableAttribute } />
    );
};
like image 138
Ori Drori Avatar answered Oct 08 '22 06:10

Ori Drori