import React, {Component} from "react"
import './index.css';
export default class ListArticle extends Component {
constructor(props) {
super(props);
this.state = {
arrayJson: [""],
}
}
get() {
fetch("http://localhost:8080/getHeaders", {
method: "GET"
})
.then(data=>data.json())
.then((data)=>{
this.setState({arrayJson:data.headerArray})
}
)
}
componentDidMount() {
this.get();
for(let i=0;i<4;i++) {
let n="";
let header = this.state.arrayJson[i];
let t=true;
for (let j = 0; j < 120; j++) {
n += header[j];
if (n === header) {
t = false;
break;
}
}
if (t) {
n += "...";
console.log(n);
} else {
console.log(n);
}
}
}
render() {
return (
<div id={"container"}>
</div>
);
}
}
this is my listArticle class
i need to get list of String values and after this I check every element and if the element length is more than 120 symbols i stop to read string element and add "..." to element
and than i put it on console.log
but problem is that console of brower shows me this
and also it gives me
And as far as I can tell, the fact is that the first element is undefined or or the list doesn’t have time to initialize, I haven’t figured it out yet
if you just try to output the list after initialization:
import React, {Component} from "react"
import './index.css';
export default class ListArticle extends Component {
constructor(props) {
super(props);
this.state = {
arrayJson: [""],
}
}
get() {
fetch("http://localhost:8080/getHeaders", {
method: "GET"
})
.then(data=>data.json())
.then((data)=>{
this.setState({arrayJson:data.headerArray})
}
)
}
componentDidMount() {
this.get();
}
render() {
return (
<div id={"container"}>
{console.log(this.state.arrayJson)}
</div>
);
}
}
2
Answers
So componentDidMount is only called once when the component is mounted, and likely before your fetch has resolved to update the state. Because of this you are only running your logic once with the state being arrayJson: [""]. You will be getting your undefined’s from the concatenated value of header[j] where i is equal to 0 making header the value of “”. This is because the index of header[0…120] don’t exist in the string “”.
After this you will get an exception as i goes higher to 1 as arrayJson: [""] only has one element (arrayJson[0]) making header equal undefined. header[0] will give you an exception as the header is undefined.
The reason why you can see it in the render function is because render will re-run each time the state or parent has changed…
I would suggest having a look into class lifecycle functions for react here, especially componentDidUpdate. Or better yet move the logic into the then section of the fetch before placing it into state. You can also eliminate the nested loop using String.slice and String.length 🙂
You’re not waiting for the
get()
to finish, soarrayJson
will be[""]
; trying to access it past the zeroth item leads to you looking at a whole bunch ofundefined
.Here’s a cleaned up version:
arrayJson
starts out asnull
, so we know it’s not loaded yetget()
is now an async function so you don’t have a pyramid of.then()
srender()
, so we can, well, eventually render it outrender()
, if the array isnull
, we do nothingtruncatedHeaders
is computed in a more functional way (I’m making some assumptions about your data format here).