Designing a star pattern in React Native












5















I was trying to draw the star patter in react native where instead of stars, there are suppose to be square boxes.



Star pattern would look like this



****
****
****
****
****


In Vanila JS, it would look like



let rows=5;
for(let i=1; i <= 5; i++) {
for(let j=1; j<=5; j++){
document.write('*');
}
document.write('<br />');
}


But that's vanila JS and I want to make the same in React-native functional component and then display it in JSX



Consider this my functional component in react native



let numberOfBoxesRequired = 4; 
let array =

const gridBoxes = (props) => {

for (let i=0; i<numberOfBoxesRequired; i++) {
for (let j=0; j<numberOfBoxesRequired; j++) {

}
}
return (
<View style={mainGridBox}>

</View>
)
}


Question: How can I do it?










share|improve this question

























  • isn't numberOfBoxesRequired already a number? Why numberOfBoxesRequired.length ?

    – Vishal Sharma
    Nov 19 '18 at 12:11











  • @VishalSharma FIxed it :) My bad ;

    – NoobieSatan
    Nov 19 '18 at 12:15
















5















I was trying to draw the star patter in react native where instead of stars, there are suppose to be square boxes.



Star pattern would look like this



****
****
****
****
****


In Vanila JS, it would look like



let rows=5;
for(let i=1; i <= 5; i++) {
for(let j=1; j<=5; j++){
document.write('*');
}
document.write('<br />');
}


But that's vanila JS and I want to make the same in React-native functional component and then display it in JSX



Consider this my functional component in react native



let numberOfBoxesRequired = 4; 
let array =

const gridBoxes = (props) => {

for (let i=0; i<numberOfBoxesRequired; i++) {
for (let j=0; j<numberOfBoxesRequired; j++) {

}
}
return (
<View style={mainGridBox}>

</View>
)
}


Question: How can I do it?










share|improve this question

























  • isn't numberOfBoxesRequired already a number? Why numberOfBoxesRequired.length ?

    – Vishal Sharma
    Nov 19 '18 at 12:11











  • @VishalSharma FIxed it :) My bad ;

    – NoobieSatan
    Nov 19 '18 at 12:15














5












5








5


1






I was trying to draw the star patter in react native where instead of stars, there are suppose to be square boxes.



Star pattern would look like this



****
****
****
****
****


In Vanila JS, it would look like



let rows=5;
for(let i=1; i <= 5; i++) {
for(let j=1; j<=5; j++){
document.write('*');
}
document.write('<br />');
}


But that's vanila JS and I want to make the same in React-native functional component and then display it in JSX



Consider this my functional component in react native



let numberOfBoxesRequired = 4; 
let array =

const gridBoxes = (props) => {

for (let i=0; i<numberOfBoxesRequired; i++) {
for (let j=0; j<numberOfBoxesRequired; j++) {

}
}
return (
<View style={mainGridBox}>

</View>
)
}


Question: How can I do it?










share|improve this question
















I was trying to draw the star patter in react native where instead of stars, there are suppose to be square boxes.



Star pattern would look like this



****
****
****
****
****


In Vanila JS, it would look like



let rows=5;
for(let i=1; i <= 5; i++) {
for(let j=1; j<=5; j++){
document.write('*');
}
document.write('<br />');
}


But that's vanila JS and I want to make the same in React-native functional component and then display it in JSX



Consider this my functional component in react native



let numberOfBoxesRequired = 4; 
let array =

const gridBoxes = (props) => {

for (let i=0; i<numberOfBoxesRequired; i++) {
for (let j=0; j<numberOfBoxesRequired; j++) {

}
}
return (
<View style={mainGridBox}>

</View>
)
}


Question: How can I do it?







javascript reactjs react-native






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 12:12







NoobieSatan

















asked Nov 19 '18 at 12:04









NoobieSatanNoobieSatan

1,136529




1,136529













  • isn't numberOfBoxesRequired already a number? Why numberOfBoxesRequired.length ?

    – Vishal Sharma
    Nov 19 '18 at 12:11











  • @VishalSharma FIxed it :) My bad ;

    – NoobieSatan
    Nov 19 '18 at 12:15



















  • isn't numberOfBoxesRequired already a number? Why numberOfBoxesRequired.length ?

    – Vishal Sharma
    Nov 19 '18 at 12:11











  • @VishalSharma FIxed it :) My bad ;

    – NoobieSatan
    Nov 19 '18 at 12:15

















isn't numberOfBoxesRequired already a number? Why numberOfBoxesRequired.length ?

– Vishal Sharma
Nov 19 '18 at 12:11





isn't numberOfBoxesRequired already a number? Why numberOfBoxesRequired.length ?

– Vishal Sharma
Nov 19 '18 at 12:11













@VishalSharma FIxed it :) My bad ;

– NoobieSatan
Nov 19 '18 at 12:15





@VishalSharma FIxed it :) My bad ;

– NoobieSatan
Nov 19 '18 at 12:15












2 Answers
2






active

oldest

votes


















2














It is the same in vanilla JavaScript and React:



const starLines = Array(4).fill('*'.repeat(4));


Once there's data, it can be output in a way that is specific to current application.



In plain JavaScript:



document.write(starLines.join('<br />'));


In React Native:



<View style={mainGridBox}>{starLines.map(line => <Text>{line}</Text>)}</View>





share|improve this answer

































    1














    First you need to render your boxes:



    let numberOfBoxesRequired = 4; 

    const gridBoxes = (props) => {
    let boxes = ;
    for (let i=0; i<numberOfBoxesRequired; i++) {
    for (let j=0; j<numberOfBoxesRequired; j++) {
    boxes.push(<Box />); // assume Box is your box component
    }

    }
    return (
    <View style={mainGridBox}>
    {boxes} // render the boxes
    </View>
    )
    }


    Then you will have to style your gridBox:



    .mainGridBox {
    flex: 1,
    flexWrap: "wrap";
    }

    .box {
    flexBasis: 0.25; // this will make a box fill 25% of the container width
    width: 30; // example width
    height: 30; // example height
    }


    This is the closest to your implementation, but I suggest you use Array.map() like estus pointed out in his answer.






    share|improve this answer
























    • @Stundiji, Trying it out but shouldn't let boxes = ; be in global scope?

      – NoobieSatan
      Nov 19 '18 at 12:44






    • 1





      We declare it in the function scope, because it is only used inside the function.

      – Stundji
      Nov 19 '18 at 12:46













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374272%2fdesigning-a-star-pattern-in-react-native%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    It is the same in vanilla JavaScript and React:



    const starLines = Array(4).fill('*'.repeat(4));


    Once there's data, it can be output in a way that is specific to current application.



    In plain JavaScript:



    document.write(starLines.join('<br />'));


    In React Native:



    <View style={mainGridBox}>{starLines.map(line => <Text>{line}</Text>)}</View>





    share|improve this answer






























      2














      It is the same in vanilla JavaScript and React:



      const starLines = Array(4).fill('*'.repeat(4));


      Once there's data, it can be output in a way that is specific to current application.



      In plain JavaScript:



      document.write(starLines.join('<br />'));


      In React Native:



      <View style={mainGridBox}>{starLines.map(line => <Text>{line}</Text>)}</View>





      share|improve this answer




























        2












        2








        2







        It is the same in vanilla JavaScript and React:



        const starLines = Array(4).fill('*'.repeat(4));


        Once there's data, it can be output in a way that is specific to current application.



        In plain JavaScript:



        document.write(starLines.join('<br />'));


        In React Native:



        <View style={mainGridBox}>{starLines.map(line => <Text>{line}</Text>)}</View>





        share|improve this answer















        It is the same in vanilla JavaScript and React:



        const starLines = Array(4).fill('*'.repeat(4));


        Once there's data, it can be output in a way that is specific to current application.



        In plain JavaScript:



        document.write(starLines.join('<br />'));


        In React Native:



        <View style={mainGridBox}>{starLines.map(line => <Text>{line}</Text>)}</View>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 '18 at 12:27

























        answered Nov 19 '18 at 12:19









        estusestus

        69k21102218




        69k21102218

























            1














            First you need to render your boxes:



            let numberOfBoxesRequired = 4; 

            const gridBoxes = (props) => {
            let boxes = ;
            for (let i=0; i<numberOfBoxesRequired; i++) {
            for (let j=0; j<numberOfBoxesRequired; j++) {
            boxes.push(<Box />); // assume Box is your box component
            }

            }
            return (
            <View style={mainGridBox}>
            {boxes} // render the boxes
            </View>
            )
            }


            Then you will have to style your gridBox:



            .mainGridBox {
            flex: 1,
            flexWrap: "wrap";
            }

            .box {
            flexBasis: 0.25; // this will make a box fill 25% of the container width
            width: 30; // example width
            height: 30; // example height
            }


            This is the closest to your implementation, but I suggest you use Array.map() like estus pointed out in his answer.






            share|improve this answer
























            • @Stundiji, Trying it out but shouldn't let boxes = ; be in global scope?

              – NoobieSatan
              Nov 19 '18 at 12:44






            • 1





              We declare it in the function scope, because it is only used inside the function.

              – Stundji
              Nov 19 '18 at 12:46


















            1














            First you need to render your boxes:



            let numberOfBoxesRequired = 4; 

            const gridBoxes = (props) => {
            let boxes = ;
            for (let i=0; i<numberOfBoxesRequired; i++) {
            for (let j=0; j<numberOfBoxesRequired; j++) {
            boxes.push(<Box />); // assume Box is your box component
            }

            }
            return (
            <View style={mainGridBox}>
            {boxes} // render the boxes
            </View>
            )
            }


            Then you will have to style your gridBox:



            .mainGridBox {
            flex: 1,
            flexWrap: "wrap";
            }

            .box {
            flexBasis: 0.25; // this will make a box fill 25% of the container width
            width: 30; // example width
            height: 30; // example height
            }


            This is the closest to your implementation, but I suggest you use Array.map() like estus pointed out in his answer.






            share|improve this answer
























            • @Stundiji, Trying it out but shouldn't let boxes = ; be in global scope?

              – NoobieSatan
              Nov 19 '18 at 12:44






            • 1





              We declare it in the function scope, because it is only used inside the function.

              – Stundji
              Nov 19 '18 at 12:46
















            1












            1








            1







            First you need to render your boxes:



            let numberOfBoxesRequired = 4; 

            const gridBoxes = (props) => {
            let boxes = ;
            for (let i=0; i<numberOfBoxesRequired; i++) {
            for (let j=0; j<numberOfBoxesRequired; j++) {
            boxes.push(<Box />); // assume Box is your box component
            }

            }
            return (
            <View style={mainGridBox}>
            {boxes} // render the boxes
            </View>
            )
            }


            Then you will have to style your gridBox:



            .mainGridBox {
            flex: 1,
            flexWrap: "wrap";
            }

            .box {
            flexBasis: 0.25; // this will make a box fill 25% of the container width
            width: 30; // example width
            height: 30; // example height
            }


            This is the closest to your implementation, but I suggest you use Array.map() like estus pointed out in his answer.






            share|improve this answer













            First you need to render your boxes:



            let numberOfBoxesRequired = 4; 

            const gridBoxes = (props) => {
            let boxes = ;
            for (let i=0; i<numberOfBoxesRequired; i++) {
            for (let j=0; j<numberOfBoxesRequired; j++) {
            boxes.push(<Box />); // assume Box is your box component
            }

            }
            return (
            <View style={mainGridBox}>
            {boxes} // render the boxes
            </View>
            )
            }


            Then you will have to style your gridBox:



            .mainGridBox {
            flex: 1,
            flexWrap: "wrap";
            }

            .box {
            flexBasis: 0.25; // this will make a box fill 25% of the container width
            width: 30; // example width
            height: 30; // example height
            }


            This is the closest to your implementation, but I suggest you use Array.map() like estus pointed out in his answer.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 '18 at 12:33









            StundjiStundji

            442212




            442212













            • @Stundiji, Trying it out but shouldn't let boxes = ; be in global scope?

              – NoobieSatan
              Nov 19 '18 at 12:44






            • 1





              We declare it in the function scope, because it is only used inside the function.

              – Stundji
              Nov 19 '18 at 12:46





















            • @Stundiji, Trying it out but shouldn't let boxes = ; be in global scope?

              – NoobieSatan
              Nov 19 '18 at 12:44






            • 1





              We declare it in the function scope, because it is only used inside the function.

              – Stundji
              Nov 19 '18 at 12:46



















            @Stundiji, Trying it out but shouldn't let boxes = ; be in global scope?

            – NoobieSatan
            Nov 19 '18 at 12:44





            @Stundiji, Trying it out but shouldn't let boxes = ; be in global scope?

            – NoobieSatan
            Nov 19 '18 at 12:44




            1




            1





            We declare it in the function scope, because it is only used inside the function.

            – Stundji
            Nov 19 '18 at 12:46







            We declare it in the function scope, because it is only used inside the function.

            – Stundji
            Nov 19 '18 at 12:46




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374272%2fdesigning-a-star-pattern-in-react-native%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?