I'm creating a status icon based on whether an object has a certain amount of errors.
What's the best way to pass the data from ComponentDidMount to the function I want to evaluate the data in.
I'm attempting to add up the status in an object, but
getting TypeError: error.filter is not a function
import React from 'react';
class PolicyIcon extends React.Component {
state = {
errors: ''
};
componentDidMount() {
let initialErrors = [];
fetch('http://localhost:3000/status/2/errors')
.then(response => {
return response.json();
})
.then(data => {
console.log(data);
this.setState({ errors: data });
});
}
addErrors = errors => {
console.log(errors);
errors.filter((x, i) => {
return x.isError >= 5;
}).length;
};
statusCircle = errors => {
return this.addErrors(errors) ? (
<svg height="100" width="100">
<circle
cx="50"
cy="50"
r="40"
stroke="black"
stroke-width="3"
fill="red"
/>
</svg>
) : (
<svg height="100" width="100">
<circle
cx="50"
cy="50"
r="40"
stroke="black"
stroke-width="3"
fill="green"
/>
</svg>
);
};
render() {
<div>{this.statusCircle(this.state.errors)}</div>;
}
}
export default UserIcon;
this.state.conditions. Also in render you have<statusCircle />, but 'statusCircle' is a function, not a component. Should have it as<div>{this.statusCircle()}</div>. (P.S. don't forget, at the moment you don't have a default state, so 'this.state.conditions' will be undefined until you set it in the API call)