Files
freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/react/add-inline-styles-in-react.md
Oliver Eyton-Williams ee1e8abd87 feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

3.1 KiB
Raw Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5a24c314108439a4d4036182 在 React 中添加内联样式 6 301378 add-inline-styles-in-react

--description--

在上一次挑战中,你可能已经注意到,除了设置为 JavaScript 对象的style属性之外,与 HTML 内联样式相比React 的内联样式还有其他几个语法差异。首先,某些 CSS 样式属性的名称使用驼峰式命名。例如,最后一个挑战用fontSize而不是font-size来设置字体的大小。对于 JavaScript 对象属性来说,像font-size这样的连字符命名是无效的语法,所以 React 使用驼峰式命名。通常,任何连字符的 style 属性在 JSX 中都是使用驼峰式命名的。

除非另有规定,否则所有属性值是长度的(如heightwidthfontSize)其单位都假定为px。例如,如果要使用em,可以用引号将值和单位括起来,例如{fontSize: "4em"}。除了默认为px的长度值之外,所有其他属性值都应该用引号括起来。

--instructions--

如果你有大量样式,你可以将 style对象分配给一个常量,以保持代码的组织有序。取消对styles常量的注释,并声明具有三个样式属性及对应值的对象。使div的文字颜色为"purple"、字号为40、边框为"2px solid purple"。然后设置style属性,使其等于styles常量。

--hints--

styles变量应该是具有三个属性的对象

assert(Object.keys(styles).length === 3);

styles变量的color属性应该设置为purple

assert(styles.color === 'purple');

styles变量应该将fontSize属性设置为40

assert(styles.fontSize === 40);

styles变量的border属性应该设置为2px solid purple

assert(styles.border === '2px solid purple');

组件应该渲染一个div元素。

assert(
  (function () {
    const mockedComponent = Enzyme.shallow(React.createElement(Colorful));
    return mockedComponent.type() === 'div';
  })()
);

div元素的样式应该由styles对象定义。

assert(
  (function () {
    const mockedComponent = Enzyme.shallow(React.createElement(Colorful));
    return (
      mockedComponent.props().style.color === 'purple' &&
      mockedComponent.props().style.fontSize === 40 &&
      mockedComponent.props().style.border === '2px solid purple'
    );
  })()
);

--seed--

--after-user-code--

ReactDOM.render(<Colorful />, document.getElementById('root'))

--seed-contents--

// Change code above this line
class Colorful extends React.Component {
  render() {
    // Change code below this line
    return (
      <div style={{color: "yellow", fontSize: 24}}>Style Me!</div>
    );
    // Change code above this line
  }
};

--solutions--

const styles = {
  color: "purple",
  fontSize: 40,
  border: "2px solid purple"
};
// Change code above this line
class Colorful extends React.Component {
  render() {
    // Change code below this line
    return (
      <div style={styles}>Style Me!</div>
    );
    // Change code above this line
  }
};