【问题标题】:React: Passing down props to functional componentsReact:将道具传递给功能组件
【发布时间】:2017-02-19 04:29:49
【问题描述】:

我有一个关于道具和功能组件的看似微不足道的问题。基本上,我有一个容器组件,它在用户单击按钮触发的状态更改时呈现模态组件。 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);
    }
};

功能(模态)组件

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>
  );
};

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

const SomeComponent = ({ someProps }) = &gt; { // ... };

【问题讨论】:

    标签: javascript reactjs components


    【解决方案1】:

    finalfreq's answer 的变体

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

    <CreateProfile
      {...this.props}
      show={this.state.showModal}
    />
    

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

    const CreateProfile = (props) => { 
    

    并单独销毁道具

    const {onFirstNameChange, onHide, show }=props;

    【讨论】:

      【解决方案2】:

      只需在源组件上执行此操作

       <MyDocument selectedQuestionData = {this.state.selectedQuestionAnswer} />
      

      然后在目标组件上执行此操作

      const MyDocument = (props) => (
        console.log(props.selectedQuestionData)
      );
      

      【讨论】:

        【解决方案3】:

        我正在使用 react 函数组件
        在父组件中首先传递 props 如下所示

        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;
        

        然后在子组件中使用 props,如下所示

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

        【讨论】:

        • 如果我想将数据从 Todo 传递到 App 怎么办?
        • 是的,请回答
        • 最好通过回调来做到这一点
        【解决方案4】:

        对上述答案的补充。

        如果React 抱怨您传递的任何propsundefined,那么您将需要使用default 值(常见于传递函数、数组或对象文字)来解构这些道具,例如

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

        【讨论】:

        • 保持得到 props.func 是未定义的,即使我知道它正在被传递。现在我知道为什么了!泰!这就是我对 React 的承诺。有时需要太多知识技巧才能使其发挥作用。
        • @friendlyfire 你稍后会掌握它的窍门。请记住,许多这些检查/控制是为了确保安全。
        【解决方案5】:

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

        <CreateProfile
          onFirstNameChange={this.firstNameChange} 
          onHide={close}
          show={this.state.showModal}
        />
        

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

        const CreateProfile = ({onFirstNameChange, onHide, show }) => {...}
        

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

        或者干脆做

        const CreateProfile = (props) => {...}
        

        并在每个地方致电props.onHide 或您尝试访问的任何道具。

        【讨论】:

        • 如何export default这个?
        • 最后写export default CreateProfile;
        • 这对我来说真的很有帮助@finalfreq 谢谢
        猜你喜欢
        • 1970-01-01
        • 2020-08-20
        • 1970-01-01
        • 2020-03-12
        • 1970-01-01
        • 2023-03-24
        • 2021-04-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多