Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate two JSX fragment or variables or string and component (in Reactjs)?

I know JSX can be very misleading because it looks like strings and it is not, thus the "string" term in the question, even if we are not really manipulating strings.

Here is a code example (wrong, obviously):

let line = <Line key={line.client_id} line={line}/>; if(line.created_at) {     return <div className="date-line"><strong>{line.created_at}</strong></div> + line; } else {     return chat_line; } 

I have a line, and I want to "concatenate" some divs in front of it under certain conditions. What would be the proper syntax? I've tried parenthesis, brackets, plus sign... None of them seem to work...

thanks

like image 390
Sephy Avatar asked Apr 28 '16 10:04

Sephy


People also ask

How do you concatenate in JSX?

To concatenate JSX elements into an array in React: Initialize an empty array. Use the push() method to push JSX elements into the array. Set the key prop on the outermost JSX element to a unique value.

How do you concatenate variables in React?

Use a template literal to concatenate strings and variables in React, e.g. "<a href={ https://example.com/${myPath} }". Template literals are delimited with backticks and allow us to embed variables and expressions using the dollar sign and curly braces ${expression} syntax.

What is JSX element []?

What is JSX? JSX stands for JavaScript XML. JSX allows us to write HTML in React. JSX makes it easier to write and add HTML in React.

What is the use of && in React?

Inline If with Logical && Operator It works because in JavaScript, true && expression always evaluates to expression , and false && expression always evaluates to false . Therefore, if the condition is true , the element right after && will appear in the output. If it is false , React will ignore and skip it.


2 Answers

Use arrays:

let lineComponent = <Line key={line.client_id} line={line}/>; if (line.created_at) {   return [     <div key="date" className="date-line"><strong>{line.created_at}</strong></div>,     lineComponent,   ]; } else {   return chat_line; } 

Or use fragments:

import createFragment from "react-addons-create-fragment";  let lineComponent = <Line key={line.client_id} line={line}/>; if (line.created_at) {   return createFragment({     date: <div className="date-line"><strong>{line.created_at}</strong></div>,     lineComponent: lineComponent,   }); } else {   return chat_line; } 

In both cases, you have to provide keys for React. In case of array, you set key directly on element. Regarding fragments, you provide key:element pairs.

NOTE: When returning from render method, you can only return single element, or NULL. Examples provided are invalid in that case.

like image 83
Andreyco Avatar answered Sep 23 '22 17:09

Andreyco


For React Native, I prefer this technique:

  1. pro: in contrast to the array technique you don't have to artificially create keys
  2. con: requires the overhead of a containing element (e.g., View, below)
jsx = <Text>first</Text>; jsx = <View>{jsx}<Text>second</Text></View>; 
like image 22
Pete Alvin Avatar answered Sep 22 '22 17:09

Pete Alvin