javascript - `style` Prop 需要从样式属性到值的映射,而不是字符串

标签 javascript reactjs

import React, { Component } from 'react';

class App extends Component {
  render() {
  const name = 'Red Header';
  const styleRed = {backgroundColor : 'red'};
  return (
    <div style="{styleRed}">
      {name}
    </div>
  );
 }
}

export default App;

那是我的代码 我正确地编写了样式对象,但 react 仍在争论中。为什么会这样 它说:

'For example, style={{marginRight: spacing + 'em'}} when using JSX.'.

我的代码看起来像上面的代码,但 React 争论不休。

最佳答案

尝试从样式 Prop 中删除“”。 https://codesandbox.io/s/6j91o6z56w

import React, { Component } from 'react';

class App extends Component {
  render() {
  const name = 'Red Header';
  const styleRed = {backgroundColor : 'red'};
  return (
    <div style={styleRed}>
      {name}
    </div>
  );
 }
}

关于javascript - `style` Prop 需要从样式属性到值的映射,而不是字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53058014/

相关文章:

javascript - 源标记和绝对 URL

reactjs - 如何让Material ui对话框显示在屏幕高度的前半部分?

reactjs - npm start 和 npm run build 失败并出现 npm ERR!代码生命周期

javascript - 在node中执行js文件

javascript - 在 Angular 中刷新指令

Javascript:一个函数,它是 IIFE 以及供以后使用的函数定义

reactjs - React useEffect 和 Axios : Making chained API calls within 'then'

javascript - React Native 映射失败

javascript - 使用 React JS 在 Firebase v9 中创建文档时保存文档 ID

javascript - 如何仅使用源 URL 而不是原始文件来调整图像大小?