I am trying to simply render each tag in my database for the user. Only the last tag is rendering (tag3) for the user. The console reads something like this:
Console:
...
Room.js:376 Tags just before .map: undefined
Room.js:376 Tags just before .map: undefined
Room.js:376 Tags just before .map: undefined
Room.js:376 Tags just before .map: undefined
Room.js:376 Tags just before .map: tag1
Room.js:376 Tags just before .map: tag2
Room.js:376 Tags just before .map: tag3
Database Structure:
>questions
>ID1234567890
>body
>title
>tags
>0
>tag: tag1
>1
>tag: tag2
>2
>tag: tag3
>tagCount: 3
>status
I've tried a couple methods of doing this, like making a <Tag> component, passing that the values and rendering them through that, but can't get the tags to render properly. I feel like I am close since the console is logging the correct values, but the render is not working. How do I get each tag to render?
Room.js:
<b>Tags:</b>
{console.log("Tags just before .map: " + this.state.questionTags)}
{
this.state.questionTags &&
(
this.state.questionTags.map((tag) => {
return (
<div>
{tag}
</div>
)
})
)
}
Setting questionTags:
//Grab tag data
fire
.database()
.ref('/questions/' + this.state.questionId + '/tags/')
.once('value')
.then(
function(snapshot) {
var tagCount = snapshot.val().tagCount;
this.setState({
tagCount: tagCount
});
var i = 0;
for (i = 0; i < tagCount; i++) {
fire
.database()
.ref('/questions/' + this.state.questionId + '/tags/' + i)
.once('value')
.then(
function(snapshot) {
this.setState({
questionTags: [snapshot.val().tag]
});
}.bind(this)
);
}
}.bind(this)
);
this.state.questionTags?questionTagseach time instead of appending the new tag to the existing array. Something else to note is that you might consider just getting all the question tags in one go rather than in a loop like that.