Jose Jose - 1 year ago 88
React JSX Question

React: Passing down props to functional components

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) {

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) {

Functional (Modal) Component

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

<div className="entry-form">
<ControlLabel>First Name</ControlLabel>
<FormControl type="text"
onChange={fields.firstNameChange} placeholder="Jane"

Example: say I want to call
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 }) = > { // ... };

Answer Source

You would need to pass down each prop individually for each function that you needed to call

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

and then in the CreateProfile component you can either do

const CreateProfile = ({firstNameChange, 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

or just do const CreateProfile = (props) => {

and in each place call props.onHide or whatever prop you are trying to access.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download