Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Rendering Article Lists with JSX

Rendering Article Lists with JSX

In this chapter you will learn to use JSX expressions to render JS data into an interface, letting the blog home page display a list of article cards.


Embedding Expressions in JSX with {}

In JSX, curly braces {} are the entry point into the JavaScript world.

Any JS expression can be placed inside curly braces: variables, operations, function calls, ternary expressions, etc.

Example

function

HomePage

(

)

{

 

const

blogTitle

=

'RUNOOB Front-End Notes'

 

const

author

=

'Xiao Ming'

 

return

(

   

<div>
<h1{blogTitle}h1>
<p>

Author:

{

author

}

p

     

<

p

Current time:

{

new

Date

(

)

.

toLocaleDateString

(

)

}

p

     

<

p

Welcome message:

{

author

?

`Hello, $

{

author

}

`

:

'Welcome, visitor'

}

p

   

div

 

)

}

The page will render:



RUNOOB 前端笔记

作者:小明

当前时间:2024/5/15

欢迎语:你好,小明

Inside curly braces you can only putExpression(code that evaluates to a value), not statements. For example {if (condition) ...} is invalid, but {condition ? 'A' : 'B'} is valid.


Attribute Binding

HTML attributes in JSX need to be dynamically bound using curly braces.

Example

function

ArticleCard

(

)

{

 

const

coverUrl

=

'/images/react-cover.png'

 

const

linkUrl

=

'/post/1'

 

return

(

   

<

div

{

/* Dynamic attributes: curly braces bind JS variables */

}

     

<

img src

=

{

coverUrl

}

alt

=

"Article cover"

/>

     

<

a href

=

{

linkUrl

}

Read more

a

     

{

/* Note: class must be written as className */

}

     

<

div className

=

"card active"

Card content

div

     

{

/* style accepts an object, with property names in camelCase */

}

     

<

div style

=

{

{

color

:

'#42b883'

,

fontSize

:

'16px'

}

}

        Green text

     

div

   

div

 

)

}

HTML JSX Description
class="btn" className="btn" class is a reserved word in JS
style="color: red" style={{ color: 'red' }} Accepts an object, property names in camelCase
src="xxx" src={variable} Use curly braces for dynamic values
onclick="fn()" onClick={fn} Event names in camelCase, pass a function reference

List Rendering: array.map()

React does not have directives like Vue v-for such directives.

To render lists in JSX, directly use JavaScript's array.map() method.

Example

function

ArticleList

(

)

{

 

// Simulate article data with a JS array of objects

 

const

articles

=

[

   

{

      id

:

1

,

      title

:

'Complete Guide to React Getting Started'

,

      summary

:

'Learn React Hooks from scratch, covering core concepts such as useState and useEffect.'

,

      date

:

'2024-05-10'

,

      category

:

'React'

,

      cover

:

'/images/react.png'

   

}

,

   

{

      id

:

2

,

      title

:

'JavaScript Asynchronous Programming Explained'

,

      summary

:

'Understand Promise, async/await, the event loop, and the microtask queue in one article.'

,

      date

:

'2024-05-08'

,

      category

:

'JavaScript'

,

      cover

:

'/images/js.png'

   

}

,

   

{

      id

:

3

,

      title

:

'CSS Grid Layout in Practice'

,

      summary

:

'Easily implement complex responsive layouts with CSS Grid.'

,

      date

:

'2024-05-05'

,

      category

:

'CSS'

,

      cover

:

'/images/css.png'

   

}

 

]

 

return

(

   

<

div className

=

"article-list"

     

{

/* map iterates the array, returning a piece of JSX for each item */

}

     

{

articles.

map

(

article

=>

(

       

<

div key

=

{

article.

id

}

className

=

"card"

         

<

img src

=

{

article.

cover

}

alt

=

{

article.

title

}

/>

         

<

div className

=

"card-body"

           

<

span className

=

"category"

{

article.

category

}

span

           

<

h3

{

article.

title

}

h3

           

<

p

{

article.

summary

}

p

           

<

span className

=

"date"

{

article.

date

}

span

         

div

       

div

     

)

)

}

   

div

 

)

}

The Importance of the key Attribute

key is a unique identifier assigned to each element in a list, helping React identify which elements have changed.

Without a key, React uses an 'in-place update' strategy, which can lead to state confusion and performance degradation.

key value Consequence
A unique id (recommended) Correctly tracks each element, efficient Diff
Array index Insertion/deletion may cause input box content to be confused
Not writing key Console warning, poor performance

Always add aunique and stablekey to the JSX returned by map. Do not use index as the key, especially when list items may be added, deleted, or reordered.


Conditional Rendering

There is no v-if in React, but there are three ways to implement conditional rendering.

Example

function

ArticlePage

(

)

{

 

const

articles

=

[

]

 

const

isLoading

=

false

 

return

(

   

<

div

{

/* Method 1: Ternary expression — suitable for choosing between two */

}

     

{

isLoading

?

(

       

<

p

Loading, please wait...

p

     

)

:

(

       

<

p

Loading complete!

p

     

)

}

     

{

/* Method 2: && short-circuit — suitable for 'show if condition, hide if not' */

}

     

{

articles.

length

===

0

&&

<

p

No articles yet; stay tuned.

p

}

     

{

/* Method 3: if/else at the top level of the component — suitable for complex multi-condition logic */

}

     

{

articles.

length

0

?

(

       

<

div

{

articles.

map

(

a

=>

<

div key

=

{

a.

id

}

{

a.

title

}

div

)

}

       

div

     

)

:

(

       

<

p

No data yet

p

     

)

}

   

div

 

)

}

Comparison of the Three Conditional Rendering Methods

Method Syntax Applicable scenarios
Ternary ? : {cond ? : } Choose between two, must return something
Short-circuit && {cond && } Show if present, hide if not
Early if check Store the JSX in a variable before return When conditional branches are complex

&& Short-circuiting has a pitfall: when the condition is the number 0, "0" will be rendered. It is recommended && Always keep the left side a boolean:{list.length > 0 && ...} instead of {list.length && ...}。


Hands-On: The Complete Blog Home Page

Example

// File path: src/App.jsx

import

'./App.css'

function

App

(

)

{

 

const

articles

=

[

   

{

      id

:

1

,

title

:

'Complete Guide to React Getting Started'

,

      summary

:

'Learn React Hooks from scratch, covering core concepts such as useState and useEffect.'

,

      date

:

'2024-05-10'

,

category

:

'React'

,

cover

:

'/images/react.png'

   

}

,

   

{

      id

:

2

,

title

:

'JavaScript Asynchronous Programming Explained'

,

      summary

:

'Understand Promise, async/await, the event loop, and the microtask queue in one article.'

,

      date

:

'2024-05-08'

,

category

:

'JavaScript'

,

cover

:

'/images/js.png'

   

}

,

   

{

      id

:

3

,

title

:

'CSS Grid Layout in Practice'

,

      summary

:

'Easily implement complex responsive layouts with CSS Grid.'

,

      date

:

'2024-05-05'

,

category

:

'CSS'

,

cover

:

'/images/css.png'

   

}

 

]

 

return

(

   

<

div className

=

"app"

     

<

header className

=

"navbar"

       

<

h1 className

=

"logo"

RUNOOB Blog

h1

       

<

nav

<

a href

=

"/"

Home

a

         

<

a href

=

"#"

About

a

       

nav

     

header

     

<

main className

=

"container"

       

<

h2 className

=

"section-title"

Latest Articles

h2

       

{

/* Conditional rendering: show when there are posts, show an empty state when there are none */

}

       

{

articles.

length

===

0

?

(

         

<

p className

=

"empty-tip"

No articles yet; stay tuned.

p

       

)

:

(

         

<

div className

=

"article-grid"

           

{

/* List rendering: map over an array */

}

           

{

articles.

map

(

article

=>

(

             

<

div key

=

{

article.

id

}

className

=

"article-card"

               

<

img src

=

{

article.

cover

}

alt

=

{

article.

title

}

className

=

"card-cover"

/>

               

<

div className

=

"card-content"

                 

<

span className

=

"card-category"

{

article.

category

}

span

                 

<

h3 className

=

"card-title"

{

article.

title

}

h3

                 

<

p className

=

"card-summary"

{

article.

summary

}

p

                 

<

span className

=

"card-date"

{

article.

date

}

span

               

div

             

div

           

)

)

}

         

div

       

)

}

     

main

     

<

footer className

=

"footer"

       

<

p

©

2024

RUNOOB Blog.

Powered

by React.

p

     

footer

   

div

 

)

}

export

default

App

Example

/* File path: append the following styles to src/App.css */

.section-title

{

 

font-size

:

24px

;

 

margin-bottom

:

20px

;

 

color

:

#333

;

}

.article-grid

{

 

display

:

grid

;

  grid-template-columns

:

repeat

(

auto-fill

,

minmax

(

280px

,

1fr

)

)

;

  gap

:

24px

;

}

.article-card

{

 

background

:

#fff

;

 

border-radius

:

12px

;

 

overflow

:

hidden

;

 

box-shadow

:

0

2px

12px

rgba

(

0

,

0

,

0

,

0.08

)

;

  transition

:

transform 0.2s

,

box-shadow

0.2s

;

 

cursor

:

pointer

;

}

.article-card

:hover

{

  transform

:

translateY

(

-4px

)

;

 

box-shadow

:

0

8px

24px

rgba

(

0

,

0

,

0

,

0.12

)

;

}

.card-cover

{

 

width

:

100%

;

 

height

:

180px

;

  object-fit

:

cover

;

}

.card-content

{

 

padding

:

16px

;

}

.card-category

{

 

display

:

inline-block

;

 

padding

:

2px

10px

;

 

background

:

#e3f2fd

;

 

color

:

#1976d2

;

 

border-radius

:

12px

;

 

font-size

:

12px

;

 

margin-bottom

:

8px

;

}

.card-title

{

 

font-size

:

18px

;

 

margin-bottom

:

8px

;

 

color

:

#222

;

}

.card-summary

{

 

font-size

:

14px

;

 

color

:

#666

;

 

line-height

:

1.6

;

 

margin-bottom

:

12px

;

}

.card-date

{

 

font-size

:

12px

;

 

color

:

#999

;

}

.empty-tip

{

 

text-align

:

center

;

 

color

:

#999

;

 

padding

:

60px

0

;

 

font-size

:

16px

;

}


Chapter Summary

In this chapter you learned four core operations in JSX:{} Embedding expressions, attribute binding (className / style),map() list rendering + key, and conditional rendering (ternary / short-circuit).

At this point, the blog home page can dynamically render article cards based on JS data.