I want to set the className based on value from an API call. The API is a financial api that returns percent change in the form of 'perchange':'(+0.03%)'. I want to figure out if it is a positive, negative or neutral. I want to affect the css to either show green when positive red for negative, and gray from neutral.
import React, { useState, useEffect } from "react";
function Ticker(props) {
const [state, setState] = useState({});
const [status, setStatus] = useState();
useEffect(() => {
let currStock = props.stock;
const options = { method: "GET", headers: { Accept: "application/json" } };
fetch(
"https://financialmodelingprep.com/api/v3/company/profile/" + currStock,
options
)
.then(response => response.json())
.then(y => {
setState(y.profile);
});
}, []);
function positive(value) {
// The Goal being to set className to these so the css can be conditional.
if (/\+/g.test(value) == true) {
setStatus("positive");
} else if (/\-/g.test(value) == false) {
setStatus("negative");
} else {
setStatus("neutral");
}
}
console.log(state);
positive(state.changesPercentage);
return (
<div className="stock">
<section className="stockInfo">
<h1>{state.companyName}</h1>
<h3> {state.industry}</h3>
<img src={state.image} />
<h3>Financials</h3>
<p className="positive">{state.changes}</p>
<p className={status}>{state.changesPercentage}</p>
<p>${state.price}</p>
<a href={state.website}>Learn More</a>
</section>
</div>
);
}
export default Ticker;
useEffect?