在const react/tyscript中调用功能
在我的应用程序中,我想有条件地呈现一些东西,因此我制作了一个函数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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在括号中包围
getState
的呼叫:如果您试图在JSX中使用任何JavaScript功能,则应期望使用此语法。
可以找到更多信息在这里 关于React中的功能。您可以向下滚动到“示例:使用箭头功能传递参数”部分,并查看一个示例,以说明如何在状态上使用映射。
You need to surround your calls to
getState
in brackets: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.