Javascript React的正则表达式未通过API调用给出正确答案

Javascript React的正则表达式未通过API调用给出正确答案,javascript,regex,reactjs,Javascript,Regex,Reactjs,基本上,当我调用API时,它返回的百分比变化为“(+0.04)”,我已经编写了正则表达式来解析下面是否有+、-,或者没有代码。我遇到的问题是,它返回的是中性而不是正确的响应 import React, { useState, useEffect } from "react"; function Ticker(props) { const [state, setState] = useState({}); const [status, setStatus] = useState("");

基本上,当我调用API时,它返回的百分比变化为“(+0.04)”,我已经编写了正则表达式来解析下面是否有+、-,或者没有代码。我遇到的问题是,它返回的是中性而不是正确的响应

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 => {
        function positive(status) {
          //   The Goal being to set className to these so the css can be conditional.
          if (/\+/g.test(status) == true) {
            return "positive";
          } else if (/\-/g.test(status) == true) {
            return "negative";
          } else {
            return "neutral";
          }
        }

        setState(y.profile);
        setStatus(positive(y.changesPercentage));
      });
  }, []);

  console.log(state);
  console.log(status);
  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>{state.changesPercentage}</p>
        <p>{state.price}</p>
        <a href={state.website}>Learn More</a>
      </section>
    </div>
  );
}

export default Ticker;
import React,{useState,useffect}来自“React”;
功能代码(道具){
const[state,setState]=useState({});
const[status,setStatus]=useState(“”);
useffect(()=>{
让currStock=props.stock;
const options={method:“GET”,头:{Accept:“application/json”};
取回(
"https://financialmodelingprep.com/api/v3/company/profile/“+货币,
选择权
)
.then(response=>response.json())
.然后(y=>{
功能正(状态){
//目标是将className设置为这些,以便css可以是有条件的。
如果(/\+/g.test(status)==true){
返回“正”;
}否则如果(/\-/g.test(status)==true){
返回“否定”;
}否则{
返回“中立”;
}
}
设置状态(y剖面);
设置状态(正(y.changesPercentage));
});
}, []);
console.log(状态);
控制台日志(状态);
返回(
{state.companyName}
{州工业}
财务数据

{state.changes}

{state.changesPercentage}

{state.price}

); } 导出默认股票代码;
您只想检查0?如果你得到的回报是中性的,它必须是0? 在这种情况下,为什么不使用不带括号的字符串,而是使用Number()

const a = '+1.05'
const b = '-9.33'
Number(a); // 1.05
Number(b); // -9.33
还有,你能试试这些正则表达式吗

.*[+]

.*[-]

您将
y.changesPercentage
传递给
positive()
函数,而不是
y.profile.changesPercentage
。除此之外,您的代码运行良好

然而,部分问题是您在
useffect()
中放置了大量不必要的内容,并且没有充分使用解构

我会这样写:

const{useState,useffect}=React;
const options={method:“GET”,头:{Accept:“application/json”};
常数符号={
“+”:“正”,
“-”:“负”,
“0”:“中性”,
};
功能正(状态){
常量[符号]=状态.匹配(/[+-]/)| |['0'];
返回标志;
}
函数代码({stock}){
const[state,setState]=useState({});
const[status,setStatus]=useState(“”);
useffect(()=>{
取回(
`https://financialmodelingprep.com/api/v3/company/profile/${stock}`,
选择权
)
.then(response=>response.json())
。然后({profile})=>{
设定状态(外形);
设置状态(正(轮廓变化百分比));
});
}, []);
控制台日志(状态);
返回(
{state.companyName}
{州工业}
财务数据

{state.changes}

{state.changesPercentage}

{state.price}

); } ReactDOM.render( , 根 );


测试正极功能后,它似乎返回正确的输出。您确定API正在获取正确的信息吗?您应该测试状态,看看它是否正在获取您想要的数据

我有一个非常新的javascript,我看到过破坏,但没有使用它。这是一个优雅的解决方案。谢谢你,我会调查更多不客气:)这篇文章-可以给你一些好的标记。