React:将道具传递给功能组件

Jos*_*ose 11 javascript components reactjs

关于道具和功能组件,我有一个看似微不足道的问题.基本上,我有一个容器组件,它在状态改变时呈现Modal组件,这是由用户单击按钮触发的.模态是一个无状态功能组件,它包含一些输入字段,这些输入字段需要连接到容器组件内部的函数.

我的问题:当用户与无状态Modal组件中的表单字段交互时,如何使用父组件内部的函数来更改状态?我错误地传递道具吗?提前致谢.

容器

export default class LookupForm extends Component {
    constructor(props) {
        super(props);

        this.state = {
            showModal: false
        };
    }
    render() {
        let close = () => this.setState({ showModal: false });

        return (
            ... // other JSX syntax
            <CreateProfile fields={this.props} show={this.state.showModal} onHide={close} />
        );
    }

    firstNameChange(e) {
      Actions.firstNameChange(e.target.value);
    }
};
Run Code Online (Sandbox Code Playgroud)

功能(模态)组件

const CreateProfile = ({ fields }) => {
  console.log(fields);
  return (
      ... // other JSX syntax

      <Modal.Body>
        <Panel>
          <div className="entry-form">
            <FormGroup>
              <ControlLabel>First Name</ControlLabel>
              <FormControl type="text"
                onChange={fields.firstNameChange} placeholder="Jane"
                />
            </FormGroup>
  );
};
Run Code Online (Sandbox Code Playgroud)

示例:说我想this.firstNameChange从Modal组件中调用.我想将道具传递给功能组件的"解构"语法让我有点困惑.即:

const SomeComponent = ({ someProps }) = > { // ... };

Shr*_*nth 24

我正在使用反应功能组件
在父组件中首先传递如下所示的道具

import React, { useState } from 'react';
import './App.css';
import Todo from './components/Todo'



function App() {
    const [todos, setTodos] = useState([
        {
          id: 1,
          title: 'This is first list'
        },
        {
          id: 2,
          title: 'This is second list'
        },
        {
          id: 3,
          title: 'This is third list'
        },
    ]);

return (
        <div className="App">
            <h1></h1>
            <Todo todos={todos}/> //This is how i'm passing props in parent component
        </div>
    );
}

export default App;
Run Code Online (Sandbox Code Playgroud)

然后使用子组件中的道具,如下所示

function Todo(props) {
    return (
        <div>
            {props.todos.map(todo => { // using props in child component and looping
                return (
                    <h1>{todo.title}</h1>
                )
            })}
        </div>  
    );
}

Run Code Online (Sandbox Code Playgroud)

  • 如果我想将数据从 Todo 传递到应用程序怎么办? (2认同)

fin*_*req 19

您需要为每个需要调用的函数单独传递每个prop

<CreateProfile
  onFirstNameChange={this.firstNameChange} 
  onHide={close}
  show={this.state.showModal}
/>
Run Code Online (Sandbox Code Playgroud)

然后在CreateProfile组件中,您可以这样做

const CreateProfile = ({onFirstNameChange, onHide, show }) => {...}
Run Code Online (Sandbox Code Playgroud)

通过解构,它将匹配的属性名称/值分配给传入的变量.名称只需与属性匹配

或者只是做

const CreateProfile = (props) => {...}
Run Code Online (Sandbox Code Playgroud)

并在每个地方打电话props.onHide或你试图访问的任何道具.

  • 最后写入导出默认CreateProfile; (2认同)

Mwa*_*ovi 6

对上述答案的补充。

如果React抱怨您传递的任何props存在undefined,那么您将需要用default值解构这些道具(如果传递函数,数组或对象文字,则常见)例如

const CreateProfile = ({
  // defined as a default function
  onFirstNameChange = f => f,
  onHide,
  // set default as `false` since it's the passed value
  show = false
}) => {...}
Run Code Online (Sandbox Code Playgroud)


MiF*_*vil 6

Finalfreq 答案的一个变体

如果你真的想要的话,你可以单独传递一些道具和所有父道具(不推荐,但有时很方便)

<CreateProfile
  {...this.props}
  show={this.state.showModal}
/>
Run Code Online (Sandbox Code Playgroud)

然后在 CreateProfile 组件中你可以这样做

const CreateProfile = (props) => { 
Run Code Online (Sandbox Code Playgroud)

并单独解构道具

const {onFirstNameChange, onHide, show }=props;
Run Code Online (Sandbox Code Playgroud)