React: Passing down props to functional components(React:将道具传递给功能组件)
问题描述
我有一个关于道具和功能组件的看似微不足道的问题.基本上,我有一个容器组件,它在用户单击按钮触发的状态更改时呈现模态组件.modal 是一个无状态的功能组件,它包含一些需要连接到容器组件内的功能的输入字段.
I have a seemingly trivial question about props and functional components. Basically, I have a container component which renders a Modal component upon state change which is triggered by user click on a button. The modal is a stateless functional component that houses some input fields which need to connect to functions living inside the container component.
我的问题:当用户与无状态模态组件内的表单字段交互时,如何使用父组件内的函数来更改状态?我是否错误地传递了道具?提前致谢.
My question: How can I use the functions living inside the parent component to change state while the user is interacting with form fields inside the stateless Modal component? Am I passing down props incorrectly? Thanks in advance.
容器
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.我猜想将道具传递给功能组件的解构"语法让我有点困惑.即:
Example: say I want to call this.firstNameChange from within the Modal component. I guess the "destructuring" syntax of passing props to a functional component has got me a bit confused. i.e:
const SomeComponent = ({ someProps }) = >{//... };
推荐答案
您需要为需要调用的每个函数单独传递每个 prop
You would need to pass down each prop individually for each function that you needed to call
<CreateProfile
onFirstNameChange={this.firstNameChange}
onHide={close}
show={this.state.showModal}
/>
然后在 CreateProfile 组件中你可以这样做
and then in the CreateProfile component you can either do
const CreateProfile = ({onFirstNameChange, onHide, show }) => {...}
通过解构,它将匹配的属性名称/值分配给传入的变量.名称只需要与属性匹配
with destructuring it will assign the matching property names/values to the passed in variables. The names just have to match with the properties
或者干脆做
const CreateProfile = (props) => {...}
并在每个地方调用 props.onHide 或您尝试访问的任何道具.
and in each place call props.onHide or whatever prop you are trying to access.
这篇关于React:将道具传递给功能组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:React:将道具传递给功能组件
基础教程推荐
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
