skip to Main Content

I want to achieve this <div>©©©©</div> using js, I tried with <div>{"©".repeat(4)}</div> but the result is one string inside the tags <div>"©©©©"</div>

2

Answers


  1. I’m assuming you want to do this programmatically, map is one solution:

      const generateNeededDiv = (number) => {
        return (
          <div>
          {
            Array.apply(null, Array(number)).map(elem => {
              return <span>&#169;</span>
            })
          }
          </div>
        )
      }
    

    Call {generateNeededDiv(4)} in the JSX where you need it. Alternatively, if you don’t want to use a function, you can take the div being returned and hardcode it to 4. The code with Array.apply(null,Array(number)) is just initializing an empty array you can iterate over.

    Login or Signup to reply.
  2. You can render an array of fragments:

    <div>{Array(4).fill(<>&#169;</>)}</div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search