I have a config file that I would like to import and render/loop through in one of my components. The config file:
data.config.js
export const data = {
clientData: {
name:'Lynda',
age:'53',
userid:7896
},
otherData: [
{
option1: 'good;',
option2: {type: 'confirmed'},
option3: ['u','g','l','y']
},
{
option1: 'awesome;',
option2: {type: 'temporary'},
option3: ['u','g']
},
],
};
component.js file
import { data } from '../config/client/data.config.js';
..
var clientData = data.clientData; // console o/p returns object key and values
var otherData = data.otherData; // console o/p returns object key and values
..
render() {
const {
title,
favicon,
socialMediaDesc,
socialMediaImg,
...
} = this.props;
...
return(
<html className="no-js" lang="en">
<title>{title}</title> // works as expectec
...
<script dangerouslySetInnerHTML={{ // eslint-disable-line react/no-danger
__html: `
for (var client in ${clientData}) {
if (${clientData}.hasOwnProperty(client)) {
(function(key, value) {
console.log(key, " : ", value);
})(client, ${clientData}[client]);
}
};
`,
}}
</html>
)
}
Expected console o/p:
name : Lynda
age : 53
userid : 7896
Result that I am getting: Uncaught SyntaxError: Unexpected identifier as value of ${clientData} is [object Object]
How can I access clientData and otherData key and values inside the render function?
import { data} from '../config/client/data.config.js';what does aconsole.log()in the constructor say?constin the middle of the return is working correctly. That is 100% invalid syntax. You should verify that you've saved and compiled your code because I promise you that doesn't work.