r/reactjs • • Jul 01 '18

Help Beginner's Thread / Easy Question (July 2018)

Hello! just helping out /u/acemarke to post a beginner's thread for July! we had almost 550 Q's and A's in last month's thread! That's 100% month on month growth! we should raise venture capital! /s

Got questions about React or anything else in its ecosystem? Stuck making progress on your app? Ask away! We’re a friendly bunch. No question is too simple. You are guaranteed a response here!

New to React? Free, quality resources here

Want Help on Code?

  • Improve your chances of getting helped by putting a minimal example on to either JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new). Describe what you want it to do, and things you've tried. Don't just post big blocks of code.
  • If you got helped, pay it forward! Answer questions even if there is already an answer - multiple perspectives can be very helpful to beginners. Also there's no quicker way to learn than being wrong on the Internet.
51 Upvotes

454 comments sorted by

View all comments

Show parent comment

1

u/nbg91 Jul 18 '18 edited Jul 18 '18

Probably a nooby way but off the top of my head:

function returnTds(numberRequired){
    const tdMarkup = [];
    for(i=0;i<numberRequired;i++){
        tdMarkup.push("<td></td>")
    }
    return tdMarkup.join("");
}

function createTable(rows, cols){
    const tdMarkup = returnTds(cols);
    const rowMarkup =  `<row>${tdMarkup}</row>`;
    const tableMarkup = ["<table>"];
    for(i=0;i<rows;i++){
        tableMarkup.push(rowMarkup);
    }
    tableMarkup.push("</table>")
    return tableMarkup.join('');
}

You could work this into one of your lifecycle methods to generate the table on render / receiving set props or whatever.

Edit:

Or a more react relevant way would be if you had a row component, the table component being passed a sets prop, then in your table jsx you can do

...
    <table>
        {
            for(i=0; i<this.props.sets; i++){
                return(
                   <Row /> 
                )
            }
        }
    </table>