【问题标题】:Expected an assignment or function call and instead saw an expression React JS期望一个赋值或函数调用,而是看到一个表达式 React JS
【发布时间】:2021-04-12 19:17:49
【问题描述】:

我正在尝试在样式中创建一个 if 语句,但我在样式 if 语句中不断收到 error Expected an assignment or function call and instead saw an expression。我试图修复放置/* eslint-disable */,但错误更改为 The `style` prop expects a mapping from style properties to values, not a string. For example, style={{marginRight: spacing + 'em'}} when using JSX.。有人可以知道我做错了什么吗?

import React, { useState } from 'react'

export default function Calendar() {

return(
        <div>
            <table id='calendario'>
              <tbody>
                {lines.map((linha) => (
                  <tr key={linha}>
                  {columns.map((coluna , e , i) => (
                  <td
                  key={coluna} onLoad={() => {
                    e < index.length ? (e = e) : e++;
                    i >= proximo_mes.length ? (i = i) : i++;
                  }}
                  style={() => {linha == 0 && coluna == e ? {filter:"brightness(0.6)"} : {filter:"brightness(1)"};
                  linha == 4 && coluna == i ? {filter:"brightness(0.6)"} : {filter:"brightness(1)"}}}
                >{dias_no_mes[indice++]}</td>
              ))}
            </tr>
          ))}
        </tbody>
      </table>
        </div>
    );
}
}

【问题讨论】:

    标签: javascript css reactjs error-handling jsx


    【解决方案1】:

    我认为 style prop 值应该类似于以下内容,因为它需要一个值

    <td
      ...
      style={{ filter: linha == 0 && coluna == e ? "brightness(0.6)" : "brightness(1)" }}
    >
    

    【讨论】:

    • 我改变了,但现在我得到了以下信息:style 道具需要从样式属性到值的映射,而不是字符串。例如,使用 JSX 时 style={{marginRight: spacing + 'em'}}。
    • 我尝试只使用一个表达式并且它有效,但我需要在样式中使用两个表达式
    • 它显示错误:期望一个赋值或函数调用,而是看到一个表达式。但是当它只有一个表达式时它的作品,但我需要样式标签内的两个表达式
    • 我的意思是你提到的有效的代码
    • style={linha == 0 && coluna == e ? {过滤器:“亮度(0.6)”}:{过滤器:“亮度(1)”}}
    猜你喜欢
    • 2020-02-12
    • 2019-10-15
    • 1970-01-01
    • 2020-11-16
    • 2022-01-14
    • 2017-09-09
    • 2021-04-19
    • 1970-01-01
    相关资源
    最近更新 更多