freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/react/set-state-with-this.setstat...

4.9 KiB
Raw Blame History

id title challengeType isRequired videoUrl localeTitle
5a24c314108439a4d4036173 Set State with this.setState 6 false 使用this.setState设置State

Description

之前的挑战包括组件state以及如何在constructor初始化状态。还有一种方法可以改变组件的state 。 React提供了一种更新名为setState组件state的方法。您可以在组件类中调用setState方法,如下所示: this.setState() ,传入一个具有键值对的对象。键是您的状态属性,值是更新的状态数据。例如,如果我们在状态中存储username并想要更新它,它将如下所示:
this.setState{
用户名:'Lewis'
};
React希望你永远不要直接修改state ,而是在状态发生变化时总是使用this.setState() 。此外您应该注意React可以批量处理多个状态更新以提高性能。这意味着通过setState方法的状态更新可以是异步的。 setState方法有一种替代语法,它提供了解决此问题的方法。这很少需要,但记住它是件好事!有关更多详细信息,请参阅React文档

Instructions

代码编辑器中有一个button元素,它有一个onClick()处理程序。当button在浏览器中收到单击事件时,将触发此处理程序,并运行MyComponent定义的handleClick方法。在handleClick方法中,使用this.setState()更新组件state 。设置name的属性state为等于字符串React Rocks! 。单击按钮并观察呈现的状态更新。如果您还不完全了解点击处理程序代码在此时的工作原理,请不要担心。它涵盖了即将到来的挑战。

Tests

tests:
  - text: '<code>MyComponent</code>的状态应使用键值对<code>{ name: Initial State }</code> 。'
    testString: 'assert(Enzyme.mount(React.createElement(MyComponent)).state("name") === "Initial State", "The state of <code>MyComponent</code> should initialize with the key value pair <code>{ name: Initial State }</code>.");'
  - text: <code>MyComponent</code>应该呈现一个<code>h1</code>标头。
    testString: 'assert(Enzyme.mount(React.createElement(MyComponent)).find("h1").length === 1, "<code>MyComponent</code> should render an <code>h1</code> header.");'
  - text: 渲染的<code>h1</code>标头应包含从组件状态呈现的文本。
    testString: '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(/<h1>TestName<\/h1>/.test(firstValue), "The rendered <code>h1</code> header should contain text rendered from the component&apos;s state."); };'
  - text: 在<code>MyComponent</code>上调用<code>handleClick</code>方法应该将state属性设置为等于<code>React Rocks!</code> 。
    testString: 'async () => { const waitForIt = (fn) => new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 250)); const mockedComponent = Enzyme.mount(React.createElement(MyComponent)); const first = () => { mockedComponent.setState({ name: "Before" }); return waitForIt(() => mockedComponent.state("name")); }; const second = () => { mockedComponent.instance().handleClick(); return waitForIt(() => mockedComponent.state("name")); }; const firstValue = await first(); const secondValue = await second(); assert(firstValue === "Before" && secondValue === "React Rocks!", "Calling the <code>handleClick</code> method on <code>MyComponent</code> should set the name property in state to equal <code>React Rocks!</code>."); };'

Challenge Seed

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      name: 'Initial State'
    };
    this.handleClick = this.handleClick.bind(this);
  }
  handleClick() {
    // change code below this line

    // change code above this line
  }
  render() {
    return (
      <div>
        <button onClick={this.handleClick}>Click Me</button>
        <h1>{this.state.name}</h1>
      </div>
    );
  }
};

After Test

console.info('after the test');

Solution

// solution required