I'm building a React component to load blog articles to insert inside a CMS. I will be getting an author name from a data-name attribute from html. When I run this code with url2 (hard coded name of the author) everything works. When I run the code with url1 (name of author read from data-attribute) nothing works. Please help.
I have this html:
<div id="dataElement" data-name="Peter Smith"></div>
And this is the code inside my react component:
constructor(props) {
super(props);
this.state = {
insightsData: [],
loading: true,
authorName: document.getElementById('dataElement').getAttribute('data-name').replace(/ /g, "_")
}
}
componentDidMount(){
let self = this;
let url2 = '/api/Insights/GetAuthorArticles?authorName=Peter_Smith&numRecords=5';
let url1 = `/api/Insights/GetAuthorArticles?authorName=${this.state.authorName}&numRecords=5`;
this.setState({loading:true});
var Promise = require("es6-promise");
Promise.polyfill();
axios.get(url)
.then((response) => {
self.setState({
insightsData: response.data.InsightsResults.Records
}, this.setState({loading:false}));
console.log(response.data.InsightsResults.Records);
});
}