在const react/tyscript中调用功能

发布于 2025-02-10 03:12:28 字数 947 浏览 4 评论 0原文

在我的应用程序中,我想有条件地呈现一些东西,因此我制作了一个函数getItem,我想在自定义工具提示中调用,const constulpooltooltip

如何使用自定义工具提示的返回调用该函数?目前,它呈现GetState(“ A”), getstate(“ b”), 工具提示上的GetState(“ C”)。请参阅下面的代码:

  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;
  }

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.

How can I call the function with the return of my custom tooltip? Currently, it render getState("A"),
getState("B"),
getState("C"), on the tooltip. 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;
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无畏 2025-02-17 03:12:29

您需要在括号中包围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;

如果您试图在JSX中使用任何JavaScript功能,则应期望使用此语法。

可以找到更多信息在这里 关于React中的功能。您可以向下滚动到“示例:使用箭头功能传递参数”部分,并查看一个示例,以说明如何在状态上使用映射。

You need to surround your calls to getState in brackets:

  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.

More information can be found here regarding functions in 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文