为什么在条件渲染中解析Math.mod()的错误?

问题描述 投票:0回答:1

以下是我的代码:

{this.state.tabledata.map(item,number => {
return (
    {(Math.mod(number,2)==0)?(  //Here parsing error
    <div
    style={{ padding: "2px",marginBottom:'5px',backgroundColor:'lightgray' }}
    class="panel panel-default"
    >
    ):(
    <div
    style={{ padding: "2px",marginBottom:'5px' }}
    class="panel panel-default"
    >
    )}

    <div
        class="panel-heading"
        style={{ backgroundColor: "transparent" }}
    >
        ....
        </div>
    </div>
    <div class="panel-body">
        <div className="row">
        ....
    </div>
    </div>
);
})}

在解析错误行时,

我尝试了number%2===0但得到了同样的错误。

谢谢。

在理解的同时让我知道是否有任何问题。

javascript reactjs
1个回答
0
投票

Math.mod不是js函数,你应该使用%。此外,您需要将map函数的参数包装到()中。试试:

{this.state.tabledata.map((item, number) => {
  return (
      {number % 2 === 0  ? (
      // rest of your code here
© www.soinside.com 2019 - 2024. All rights reserved.