Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在变量中获取此值并在React中的条件中使用它?_Javascript_Reactjs_Typescript_Jsx - Fatal编程技术网

Javascript 如何在变量中获取此值并在React中的条件中使用它?

Javascript 如何在变量中获取此值并在React中的条件中使用它?,javascript,reactjs,typescript,jsx,Javascript,Reactjs,Typescript,Jsx,如果能帮我解决这个问题,我将不胜感激。在下面的代码中,我想从span中获取介于``之间的值,并将它们分配给一个变量,以便具有以下逻辑: ${research.data.codesRelated[1].code} === undefined ? ${research.data.description} : ${research.data.codesRelated[1].code} ${research.data.description} 我该怎么做?这看起来很简单,但我无法理清它,不

如果能帮我解决这个问题,我将不胜感激。在下面的代码中,我想从span中获取介于``之间的值,并将它们分配给一个变量,以便具有以下逻辑:

${research.data.codesRelated[1].code} === undefined 
  ? ${research.data.description} 
  : ${research.data.codesRelated[1].code} ${research.data.description}
我该怎么做?这看起来很简单,但我无法理清它,不知道在代码中放置const和conditional呈现并使其工作。每个人都要提前

代码如下:

const Research: FC<ResearchProps> = memo(
  ({ research, hideLabel = false, intl }) => (
    <div className="section research">
       <div className="section__value" id="container-research-value">
        <div className="research-info">
          <div className="description" id="element-research-description">
            <PopoverWrapper
              id="element-research-description-tooltip"
              trigger="hover"
              placement="top"
              speaker={<span className="text-tooltip">{research.data.description}</span>}
            >
              **<span>{`${research.data.codesRelated[1].code} ${research.data.description}`}</span>**
            </PopoverWrapper>
          </div>
const Research:FC=memo(
({research,hideLabel=false,intl})=>(
**{`${research.data.codesRelated[1].code}${research.data.description}`}**
您可以试试这个

const Research:FC=memo(
({research,hideLabel=false,intl})=>(
{research.data.codesRelated[1]。代码===未定义?
研究、数据、描述
: 
{research.data.codesRelated[1].code}{research.data.description}
}

如前所述,您可以使用三元运算符

您可以使用一个简单的技巧:对字符串模板末尾有空格的代码进行条件呈现

<span>
{research.data.codesRelated[1].code && `${research.data.codesRelated[1].code} `}
{`${research.data.description}`}
</span>

{research.data.codesRelated[1]。代码&`${research.data.codesRelated[1]。代码}`}
{`${research.data.description}`}
但创建外部函数以生成此字符串的最佳方法。渲染如下:

<span>
{buildResearchDescription(research.data)}
</span>

{buildResearchDescription(research.data)}

您可以将其放在那里,尽管我不这么认为--最好在渲染之前拉出字符串。您的代码段无法运行,
Uncaught SyntaxError:const声明中缺少初始值设定项
谢谢您的回答,但这似乎不是在验证research.data.codesRelated[1]。code==undefinedconst research={data:{description:“Some description”,codesRelated:[{},{}]},}使用此数据结构,它可以正常工作。