Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an ordered numbered list in React

Tags:

reactjs

I need to create an ordered list in React and I am doing following in the render()

render() {
    <div key ={category.index}>
        question.map(questionlist =>
        <li key={questionlist.key}>{questionlist.description}</li>)}
    </div>
}
like image 863
early_ninja Avatar asked Oct 23 '25 16:10

early_ninja


2 Answers

Use an ordered list to get the numbering

render() {
  <ol>
     {question.map(questionlist =>
       <li key={questionlist.key}>{questionlist.description}</li>
     )}
  </ol>
}
like image 121
Charlie Martin Avatar answered Oct 25 '25 08:10

Charlie Martin


You need to use curly braces {} if you want to use JavaScript inside your JSX. Here is a demo: http://codepen.io/PiotrBerebecki/pen/RGjLyd

Based on your question, I've assumed that your array has the following format:

const questionList = [
  {description: 'How to render list in React?', key: 0},
  {description: 'Do you like JS?', key: 1},
  {description: 'Do you know CSS?', key: 2}
];

Full code here:

class App extends React.Component {

  render() {
    const questionList = [
      {description: 'How to render list in React?', key: 0},
      {description: 'Do you like JS?', key: 1},
      {description: 'Do you know CSS?', key: 2}
    ];

    return (
      <ol>
        {questionList.map(question => {
          return (
            <li key={question.key}>{question.description}</li>
          );
        })}
      </ol>
    );
  }
}

ReactDOM.render(
  <App />,
  document.getElementById('app')
);
like image 43
Piotr Berebecki Avatar answered Oct 25 '25 08:10

Piotr Berebecki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!