React - use dynamic array for loop element

0 votes
41 views
added Feb 1 in React by lcjr First Warrant Officer (11,850 points)

Dynamic array usage:

// non-dynamic array
const suggestionItems = ['Test 123', 'Test 234', 'Test 456'];


//dynamic array
const latestKeyedText =
  cookies && cookies.searchHistory && cookies.searchHistory.length > 0
    ? `${cookies.searchHistory[cookies.searchHistory.length - 1]}`
    : null;

const suggestionItems = latestKeyedText
? [latestKeyedText, `${latestKeyedText}1`, `${latestKeyedText}2`, `${latestKeyedText}3`]
: [];

 

Use in components:

//here's the array map in loop:
<div className="bottom_buddyPrompt_suggestion">
  {suggestionItems.map((item, index) => (
    <div key={index} className="buddyPrompt_bubble">
      {item}
    </div>
  ))}
</div>

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...