简体   繁体   English

如何在 html/jsx reactjs 中添加脚本

[英]how to add script in html/jsx reactjs

I'm trying to add a style to a html element only when the property of an array of object has a certain value.仅当 object 数组的属性具有特定值时,我才尝试向 html 元素添加样式。 but I got an error like this instead:但我得到了这样的错误:

/src/App.js: Unexpected token, expected "..." (35:25)

here's my code: codesandbox这是我的代码: codesandbox

export default function App() {
  const data = [
    {
      id:1,
      score:10,
    },
    {
      id:2,
      score:20,
    },
    {
      id:3,
      score:50,
    }
  ];

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      {
        data.length > 0
        ? (
          <table>
            <thead>
              <tr>
                <th>No.</th>
                <th>value</th>
              </tr>
            </thead>
            <tbody>
              {
                data.map((item, index) => (
                  <tr key={item.id}>
                    <td>{index+1}.</td>
                    <td {item.value > 10 ? 'style=\'backgroundColor:"red"\'' : ''}>
                      {item.value}
                    </td>
                  </tr>
                ))
              }
            </tbody>
          </table>
        )
        : (
          <div>
            <em>No data</em>
          </div>
        )
      }
      {
        data.map((item, index) => (

        ))
      }
    </div>
  );
}
<td style={item.value > 10 ? { backgroundColor: "red" } : ""}>

The style attribute in React accepts a JavaScript object with camelCased properties rather than a CSS string. React 中的 style 属性接受带有驼峰式属性的 JavaScript object 而不是 CSS 字符串。 You can find more details here .您可以在此处找到更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM