简体   繁体   English

在 const React/Typescript 中调用函数

[英]Call a function inside const React/Typescript

In my application, I want to conditionally render something, so I made a function getItem which I want to call in my custom Tooltip, const CustomTooltip .在我的应用程序中,我想有条件地渲染一些东西,所以我创建了一个函数getItem ,我想在我的自定义工具提示const CustomTooltip中调用它。

How can I call the function with the return of my custom tooltip?如何在return自定义工具提示的情况下调用该函数? Currently, it render getState("A"), getState("B"), getState("C"), on the tooltip.目前,它在工具提示上呈现 getState("A")、getState("B")、getState("C")。 See code below:请参见下面的代码:

  const numberStates = 3;
  function getState({payload}: TooltipProps<ValueType, NameType>, state: string ){
    if(payload){
      for(let i = 0; i < numberStates; i++){
        if(payload[i].dataKey == state){
          return <p>{ payload[i] } : { payload[i].value }</p>
        }
      }
    }
    return null;
  }


  const CustomTooltip = ({ active, payload, label}: TooltipProps<ValueType, NameType>) => {
    if(active && payload && payload.length){
      return (
        <div className = "custom-tooltip">
          getState("A")
          getState("B")
          getState("C")
        </div>
      );
    }
    return null;
  }

You need to surround your calls to getState in brackets:您需要将调用getState括在括号中:

  const CustomTooltip = ({ active, payload, label}: TooltipProps<ValueType, NameType>) => {
    if(active && payload && payload.length){
      return (
        <div className = "custom-tooltip">
          {getState("A")}
          {getState("B")}
          {getState("C")}
        </div>
      );
    }
    return null;

You should expect to use this syntax if you're trying to utilize any javascript functionalities within the JSX.如果您尝试使用 JSX 中的任何 javascript 功能,您应该期望使用此语法。

More information can be found here regarding functions in react.可以在此处找到有关 react 中的功能的更多信息。 You can scroll down to the "Example: Passing params using arrow functions" section and see an example of how brackets are necessary for using a map on the state.您可以向下滚动到“示例:使用箭头函数传递参数”部分,并查看如何在状态上使用地图时需要括号的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用React Redux在const click函数中调用函数? - How to call a function inside the const click function using React Redux? React(打字稿):如何在 const 创建中循环 - React (typescript): How to loop inside const creation 在 function 中进行 api 调用导致浏览器在反应中冻结(打字稿) - Making an api call inside a function causing the browser to freeze in react (typescript) 如何调用 typescript class 中反应组件内部定义的 function? - How to call a function defined inside react component in a typescript class? const javascript内的调用函数反应本机 - Call Function within const javascript react native 全局变量在 const function React 中未定义 - Global variable is undefined inside a const function React 在const内部定义函数 - Define function inside const react native Javascript/Typescript 将默认常量导出为来自异步 function 调用的值 - Javascript/Typescript Export Default Const as value from async function call 为什么在 function 调用之前需要“...”,该调用在 map function 中作为表达式传递 - Why is "..." required before a function call that is being passed as an expression inside map function in React with typescript 如何在 jquery 函数中调用 typescript 函数? - how to call a typescript function inside a jquery function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM