--- id: 5a24c314108439a4d403617b title: Pass a Callback as Props challengeType: 6 isRequired: false videoUrl: '' localeTitle: 将回调作为道具传递 --- ## Description
您可以将state作为道具传递给子组件,但您不仅限于传递数据。您还可以将处理函数或在React组件上定义的任何方法传递给子组件。这是允许子组件与其父组件交互的方式。您可以将方法传递给孩子,就像常规道具一样。它被分配了一个名称,您可以在子组件中的this.props下访问该方法名称。
## Instructions
代码编辑器中列出了三个组件。 MyApp组件是将呈现GetInputRenderInput子组件的父组件。将GetInput组件添加到MyApp的render方法,然后从MyAppstate向它传递一个名为input的prop,该input分配给inputValue 。还要创建一个名为handleChange的prop,并将输入处理程序handleChange给它。接下来,将RenderInput添加到MyApp的render方法,然后创建一个名为input的prop,并将inputValuestate传递给它。完成后,您将能够在GetInput组件中input字段,然后通过props调用其父级中的处理程序方法。这将更新父级state的输入,该输入作为props传递给两个子级。观察数据如何在组件之间流动以及单个事实源如何保持父组件的state 。不可否认,这个例子有点人为,但应该用来说明如何在React组件之间传递数据和回调。
## Tests
```yml tests: - text: MyApp组件应该呈现。 testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyApp)); return mockedComponent.find("MyApp").length === 1; })(), "The MyApp component should render.");' - text: GetInput组件应该呈现。 testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyApp)); return mockedComponent.find("GetInput").length === 1; })(), "The GetInput component should render.");' - text: RenderInput组件应该呈现。 testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyApp)); return mockedComponent.find("RenderInput").length === 1; })(), "The RenderInput component should render.");' - text: GetInput组件应该将MyApp状态属性inputValue作为props接收,并包含一个修改MyApp状态的input元素。 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 GetInput component should receive the MyApp state property inputValue as props and contain an input element which modifies MyApp state."); }; ' - text: RenderInput组件应该将MyApp状态属性inputValue作为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 RenderInput component should receive the MyApp state property inputValue as props."); }; ' ```
## Challenge Seed
```jsx 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 (
{ /* change code below this line */ } { /* change code above this line */ }
); } }; class GetInput extends React.Component { constructor(props) { super(props); } render() { return (

Get Input:

); } }; class RenderInput extends React.Component { constructor(props) { super(props); } render() { return (

Input Render:

{this.props.input}

); } }; ```
### After Test
```js console.info('after the test'); ```
## Solution
```js // solution required ```