Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Designing a star pattern in React Native

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?

like image 618
iRohitBhatia Avatar asked Nov 19 '18 12:11

iRohitBhatia


People also ask

How to make a react native app with npm?

We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

Can I change the color of my React-Native project?

In case if you are working on a real React-Native Project and the designer meant to change the color you can quickly adapt to a new design using these design patterns practices. We will create a simple Welcome Screen in our application.

Why should you learn design patterns in React-Native for mobile app development?

Developing Android & iOS apps has never been easier when it comes to delivering the right product to the end-user. React-Native has changed this problem totally since it was introduced, and knowing about design patterns in React-Native is an essential skill that developers should know.

Can I use React Native vector icons instead of React Native Expos?

If you’re using a non-Expo React Native project, you can use React Native Vector Icons as a direct replacement. Of course, feel free to use whichever icon library you like instead!


2 Answers

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>
like image 108
Estus Flask Avatar answered Oct 08 '22 05:10

Estus Flask


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.

like image 2
Stundji Avatar answered Oct 08 '22 04:10

Stundji