Files
freeCodeCamp/curriculum/challenges/chinese-traditional/03-front-end-development-libraries/react/render-state-in-the-user-interface-another-way.md

2.9 KiB
Raw Permalink Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5a24c314108439a4d4036172 以另一種方式在用戶界面中渲染狀態 6 301408 render-state-in-the-user-interface-another-way

--description--

還有另一種方法可以訪問組件中的 state。 在 render() 方法中,在 return 語句之前,可以直接編寫 JavaScript。 例如,可以聲明函數、從 stateprops 中訪問數據、對此數據執行計算等。 然後,可以將任何數據賦值給 return 語句中可以訪問的變量。

--instructions--

MyComponent 的 render 方法中,定義一個名爲 nameconst(常量),並將其設置爲組件 state 中的 name 值。 因爲可以直接在代碼部分編寫 JavaScript所以不需要用大括號括起來。

接下來,在 return 語句中,在 h1 標籤中渲染變量 name 的值。 記住,在 return 語句中需要使用 JSX 語法(用到 JavaScript 的花括號)。

--hints--

MyComponent 應該有一個鍵 name,其值 freeCodeCamp 存儲在其 state 中。

assert(
  Enzyme.mount(React.createElement(MyComponent)).state('name') ===
    'freeCodeCamp'
);

MyComponent 應該在 div 中渲染一個 h1 標題元素。

assert(
  /<div><h1>.*<\/h1><\/div>/.test(
    Enzyme.mount(React.createElement(MyComponent)).html()
  )
);

渲染的 h1 標籤應該包含 {name} 的引用。

(getUserInput) =>
  assert(/<h1>\n*\s*\{\s*name\s*\}\s*\n*<\/h1>/.test(getUserInput('index')));

渲染的 h1 標題元素應包含從組件狀態渲染的文本。

async () => {
  const waitForIt = (fn) =>
    new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 250));
  const mockedComponent = Enzyme.mount(React.createElement(MyComponent));
  const first = () => {
    mockedComponent.setState({ name: 'TestName' });
    return waitForIt(() => mockedComponent.html());
  };
  const firstValue = await first();
  assert(firstValue === '<div><h1>TestName</h1></div>');
};

--seed--

--after-user-code--

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

--seed-contents--

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      name: 'freeCodeCamp'
    }
  }
  render() {
    // Change code below this line

    // Change code above this line
    return (
      <div>
        { /* Change code below this line */ }

        { /* Change code above this line */ }
      </div>
    );
  }
};

--solutions--

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      name: 'freeCodeCamp'
    }
  }
  render() {
    // Change code below this line
    const name = this.state.name;
    // Change code above this line
    return (
      <div>
        { /* Change code below this line */ }
        <h1>{name}</h1>
        { /* Change code above this line */ }
      </div>
    );
  }
};