Designing a star pattern in React Native
up vote
6
down vote
favorite
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
add a comment |
up vote
6
down vote
favorite
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
isn'tnumberOfBoxesRequired
already a number? WhynumberOfBoxesRequired.length
?
– Vishal Sharma
Nov 19 at 12:11
@VishalSharma FIxed it :) My bad ;
– KuchBhi
Nov 19 at 12:15
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
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
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
javascript reactjs react-native
edited Nov 19 at 12:12
asked Nov 19 at 12:04
KuchBhi
626218
626218
isn'tnumberOfBoxesRequired
already a number? WhynumberOfBoxesRequired.length
?
– Vishal Sharma
Nov 19 at 12:11
@VishalSharma FIxed it :) My bad ;
– KuchBhi
Nov 19 at 12:15
add a comment |
isn'tnumberOfBoxesRequired
already a number? WhynumberOfBoxesRequired.length
?
– Vishal Sharma
Nov 19 at 12:11
@VishalSharma FIxed it :) My bad ;
– KuchBhi
Nov 19 at 12:15
isn't
numberOfBoxesRequired
already a number? Why numberOfBoxesRequired.length
?– Vishal Sharma
Nov 19 at 12:11
isn't
numberOfBoxesRequired
already a number? Why numberOfBoxesRequired.length
?– Vishal Sharma
Nov 19 at 12:11
@VishalSharma FIxed it :) My bad ;
– KuchBhi
Nov 19 at 12:15
@VishalSharma FIxed it :) My bad ;
– KuchBhi
Nov 19 at 12:15
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
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>
add a comment |
up vote
1
down vote
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.
@Stundiji, Trying it out but shouldn'tlet boxes = ;
be in global scope?
– KuchBhi
Nov 19 at 12:44
1
We declare it in the function scope, because it is only used inside the function.
– Stundji
Nov 19 at 12:46
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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>
add a comment |
up vote
2
down vote
accepted
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>
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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>
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>
edited Nov 19 at 12:27
answered Nov 19 at 12:19
estus
63.5k2193200
63.5k2193200
add a comment |
add a comment |
up vote
1
down vote
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.
@Stundiji, Trying it out but shouldn'tlet boxes = ;
be in global scope?
– KuchBhi
Nov 19 at 12:44
1
We declare it in the function scope, because it is only used inside the function.
– Stundji
Nov 19 at 12:46
add a comment |
up vote
1
down vote
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.
@Stundiji, Trying it out but shouldn'tlet boxes = ;
be in global scope?
– KuchBhi
Nov 19 at 12:44
1
We declare it in the function scope, because it is only used inside the function.
– Stundji
Nov 19 at 12:46
add a comment |
up vote
1
down vote
up vote
1
down vote
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.
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.
answered Nov 19 at 12:33


Stundji
31618
31618
@Stundiji, Trying it out but shouldn'tlet boxes = ;
be in global scope?
– KuchBhi
Nov 19 at 12:44
1
We declare it in the function scope, because it is only used inside the function.
– Stundji
Nov 19 at 12:46
add a comment |
@Stundiji, Trying it out but shouldn'tlet boxes = ;
be in global scope?
– KuchBhi
Nov 19 at 12:44
1
We declare it in the function scope, because it is only used inside the function.
– Stundji
Nov 19 at 12:46
@Stundiji, Trying it out but shouldn't
let boxes = ;
be in global scope?– KuchBhi
Nov 19 at 12:44
@Stundiji, Trying it out but shouldn't
let boxes = ;
be in global scope?– KuchBhi
Nov 19 at 12:44
1
1
We declare it in the function scope, because it is only used inside the function.
– Stundji
Nov 19 at 12:46
We declare it in the function scope, because it is only used inside the function.
– Stundji
Nov 19 at 12:46
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
isn't
numberOfBoxesRequired
already a number? WhynumberOfBoxesRequired.length
?– Vishal Sharma
Nov 19 at 12:11
@VishalSharma FIxed it :) My bad ;
– KuchBhi
Nov 19 at 12:15