freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/react/pass-a-callback-as-props.ch...

5.5 KiB
Raw Blame History

id title challengeType isRequired videoUrl localeTitle
5a24c314108439a4d403617b Pass a Callback as Props 6 false 将回调作为道具传递

Description

您可以将state作为道具传递给子组件但您不仅限于传递数据。您还可以将处理函数或在React组件上定义的任何方法传递给子组件。这是允许子组件与其父组件交互的方式。您可以将方法传递给孩子就像常规道具一样。它被分配了一个名称您可以在子组件中的this.props下访问该方法名称。

Instructions

代码编辑器中列出了三个组件。 MyApp组件是将呈现GetInputRenderInput子组件的父组件。将GetInput组件添加到MyApp的render方法然后从MyAppstate向它传递一个名为input的propinput分配给inputValue 。还要创建一个名为handleChange的prop并将输入处理程序handleChange给它。接下来,将RenderInput添加到MyApp的render方法然后创建一个名为input的prop并将inputValuestate传递给它。完成后,您将能够在GetInput组件中input字段然后通过props调用其父级中的处理程序方法。这将更新父级state的输入该输入作为props传递给两个子级。观察数据如何在组件之间流动以及单个事实源如何保持父组件的state 。不可否认这个例子有点人为但应该用来说明如何在React组件之间传递数据和回调。

Tests

tests:
  - text: <code>MyApp</code>组件应该呈现。
    testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyApp)); return mockedComponent.find("MyApp").length === 1; })(), "The <code>MyApp</code> component should render.");'
  - text: <code>GetInput</code>组件应该呈现。
    testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyApp)); return mockedComponent.find("GetInput").length === 1; })(), "The <code>GetInput</code> component should render.");'
  - text: <code>RenderInput</code>组件应该呈现。
    testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyApp)); return mockedComponent.find("RenderInput").length === 1; })(), "The <code>RenderInput</code> component should render.");'
  - text: <code>GetInput</code>组件应该将<code>MyApp</code>状态属性<code>inputValue</code>作为props接收并包含一个修改<code>MyApp</code>状态的<code>input</code>元素。
    testString: 'async () => { const waitForIt = (fn) => new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 250)); const mockedComponent = Enzyme.mount(React.createElement(MyApp)); const state_1 = () => { mockedComponent.setState({inputValue: ""}); return waitForIt(() => mockedComponent.state() )}; const state_2 = () => { mockedComponent.find("input").simulate("change", {target: {value: "TestInput"}}); return waitForIt(() => mockedComponent.state() )}; const updated_1 = await state_1(); const updated_2 = await state_2(); assert(updated_1.inputValue === "" && updated_2.inputValue === "TestInput", "The <code>GetInput</code> component should receive the <code>MyApp</code> state property <code>inputValue</code> as props and contain an <code>input</code> element which modifies <code>MyApp</code> state."); }; '
  - text: <code>RenderInput</code>组件应该将<code>MyApp</code>状态属性<code>inputValue</code>作为props接收。
    testString: 'async () => { const waitForIt = (fn) => new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 250)); const mockedComponent = Enzyme.mount(React.createElement(MyApp)); const state_1 = () => { mockedComponent.setState({inputValue: "TestName"}); return waitForIt(() => mockedComponent )}; const updated_1 = await state_1(); assert(updated_1.find("p").text().includes("TestName"), "The <code>RenderInput</code> component should receive the <code>MyApp</code> state property <code>inputValue</code> as props."); }; '

Challenge Seed

class MyApp extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      inputValue: "
    }
    this.handleChange = this.handleChange.bind(this);
  }
  handleChange(event) {
    this.setState({
      inputValue: event.target.value
    });
  }
  render() {
    return (
       <div>
        { /* change code below this line */ }

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

class GetInput extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <div>
        <h3>Get Input:</h3>
        <input
          value={this.props.input}
          onChange={this.props.handleChange}/>
      </div>
    );
  }
};

class RenderInput extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <div>
        <h3>Input Render:</h3>
        <p>{this.props.input}</p>
      </div>
    );
  }
};

After Test

console.info('after the test');

Solution

// solution required