gpt4 book ai didi

javascript - 如何渲染 jsx 以响应条件为真和数组映射?

转载 作者:行者123 更新时间:2023-11-30 10:58:32 25 4
gpt4 key购买 nike

我想在某些条件为真时渲染一个 JSX,并通过一个数组进行映射。

下面是代码,

{this.props.variables &&
this.props.variable.map((variable, index) => {
let element;
if (variable.anchor) {
element = document.querySelector(variable.anchor);
}
this.box_ref.current && element && (// error here
<Childcomponent
element1={this.box_ref.current}
anchor={variable.anchor}
/>)
}
}
)
}

有一个错误指出表达式不是赋值或调用。我该如何解决?谢谢。

最佳答案

您需要为 #Array.map callback 提供返回值.

此外,您应该为数组中的 React 元素提供唯一键:

<>
{this.props.variables &&
this.props.variable.map((variable, index) => {
let element;
if (variable.anchor) {
element = document.querySelector(variable.anchor);
}
// v Add return statement
return (
this.box_ref.current &&
element && (
<Childcomponent
key={index}
element1={this.box_ref.current}
anchor={variable.anchor}
/>
)
);
})}
</>

关于javascript - 如何渲染 jsx 以响应条件为真和数组映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59033236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com