I have a JSON file for my data:
{
"EIC": {
"id": "EIC",
"text": "Want to do a quick word game?",
"replies": ["Sure", "Later"]
},
"YMB": {
"id": "YMB",
"text": "Okay, tomorrow. Cya!",
"replies": ["bye Woebot"],
}
}
I want to render the array of replies so we can see the different answers possible in the HTML.
Here's my component at the moment:
import React from 'react';
import axios from 'axios';
import style from "../styles/entry.css";
import * as allornothing from '../allornothing.json';
class EntryList extends React.Component {
constructor(props) {
super(props);
this.state = {
list: Object.values(allornothing)
}
}
renderReplies(replies) {
return replies.map((reply) => {
return (
<div class="entry-body col-12">
<button class="back">{ reply }</button>
</div>
)
})
}
render() {
return (
<div class="row" id="entries-list">
{this.state.list.map(function(entry) {
return (
<div class="col-md-4 col-sm-12">
<div class="entry col-12">
<div class="row">
<div class="entry-header col-12">
<div class="entry-id">
<span>{entry.id}</span>
</div>
<p>{entry.text}</p>
</div>
{ this.renderReplies(entry.replies) }
<div class="entry-manage col-12">
<div class="row">
<span class="manage-button">
<i class="fas fa-comments"></i> Add new answer
</span>
<span class="manage-button">
<i class="fas fa-pencil-alt"></i> Modify
</span>
</div>
</div>
</div>
</div>
</div>
)
})}
</div>
)
}
}
export default EntryList
But I get this error:
TypeError: Cannot read property 'renderReplies' of undefined
While, if I replace { this.renderReplies(entry.replies) } by console.log(entry.replies) it does print the replies of every question.
I've also tried this solution before coming up with the renderReplies function but I just get the same error...
Also if I put {entry.replies} in my html code, it does render a list of strings too.
Does anyone have an idea of why I get this error and how I can display my list of replies? Thanks.
this.renderReplies = this.renderReplies.bind(this)