pass props with functional component without declaring function within component

pass props from functional component to class component
react pass props to component
react functional components
react call function from another component
how to pass a function from one component to another in react
react component
pass function as props react native
function component pass function

I iterate over a list of items and want to call a function onClick(). Example below:

class App extends Component {
    state = { items: [1, 2, 3, 4] };

    click = i => console.log(i);

    render() {
        return (
            <div>
                {this.state.items.map(item => (
                    <div 
                        key={item} 
                        // this works fine, but I rather not declare functions within the render function / JSX
                        onClick={() => this.click(item)}
                    >
                        {`click on # ${item}`}
                    </div>
                ))}
            </div>
        )
    }
}

As you can see, I declare a function in the JSX. I do this over and over, but some time ago I learned that you should avoid declaring a function within the component JSX itself, for performance reasons.

Is this true, also for functional components? And if so, is there any other way to pass dynamic information (item, in this case) to a callback?

NOTE: I'm looking forward to using hooks at some point, but right now I just want to know a best practice without using them.

NOTE II: That being said, if you are sure that this is a relevant problem and it could not be solved until now because of hooks, I obviously would like to learn that :)

Is this true, also for functional components?

Well actually there is no functional component used in your code. And everything you do in a function that gets called very often (render() for example) causes performance to decrease, no matter wether that is a function or variable declaration or invokation. Wether that decrease matters is another thing.

And if so, is there any other way to pass dynamic information (item, in this case) to a callback?

You could .bind(...) it:

 onClick = {console.log.bind(console, item) }

but really, did you notice any delay on a rerender? Probably not, and if so that is not caused by the function declaration. Write code that looks beautiful to you, don't optimize for the compiler, let the compiler do that.

but some time ago I learned that you should avoid declaring a function within the component JSX itself

You shouldn't really avoid it, rather prefer other ways if possible. In this case there is not really a better way so go with it.

React: How To Access Props In A Functional Component, now have the ability have a states and lifecycle methods. Of course, you can still pass props from parent to child with functional components but the big difference is that you’ll need to declare them in your functional component callback just as you

You can declare another method like this. Don't forget to use .bind. Otherwise, the method won't be called correctly.

class App extends Component {
    state = { items: [1, 2, 3, 4] };

    handleClick(item) {
        // do something
        console.log(item);
    }
    render() {
        return (
            <div>
                {this.state.items.map(item => (
                    <div 
                        key={item} 
                        // this works fine, but I rather not declare functions within the render function / JSX
                        onClick={this.handleClick.bind(this, item)}
                    >
                        {`click on # ${item}`}
                    </div>
                ))}
            </div>
        )
    }
}

How to pass props to components in React, The CurrentDate component should render the value from the date prop in the p tag. .com/questions/41813165/passing-accessing-props-in-stateless-child-​component So, in this function you now have an object called props with a property date, which means the OP solved it without seeing the full working solution. Passing Props to a Functional Child Component But unlike class components (which are ES6 classes), functional components cannot have constructor () or naturally inherit props from the parent components. You have to pass props as an argument (that is what it is originally) to the functional component.

You can create a parent component which will be responsible for looping through the items and passing the click handler down as a prop to each individual child component.

Then inside the child component you can easily reference the handler as this.props.onClick

Functions (objects) are passed by reference in JavaScript. By declaring it in the parent scope it will only take up space there. If initialized in the child, it will create space in memory for the function with each child component.

class Parent extends Component {
    state = { items: [1, 2, 3, 4] };

    parentClick(item) {
        // do something
        console.log(item);
    }
    render() {
        return (
            <div>
                {this.state.items.map(item => (
                  <Child item={item} onClick={this.parentClick}>
                ))}
            </div>
        )
    }
}



class Child extends Component {

    childClick(item) {
        this.props.onClick(item)
    }

    render() {
        return (
            <div>
                <p onClick={this.childClick(this.props.item)}>this.props.item</p>
            </div>
        )
    }
}

States and componentDidMount() in functional components with , Note: In case a Function Component is not to console log state and props of the component. If both don't change, there is no new render You can also specify when the Effect Hook should run depending on the variables you pass into the array as second argument. Passing only props from component to component doesn't make the component interactive, because nothing is there to change the props. Props are read-only. That's the time when React State comes into play which can be changed.

Pass Props to a Stateless Functional Component - JavaScript, Just like your parents gave you your eye color your components in React give properties to their children. Of course, you can still pass props from parent to child with functional components but need to declare them in your functional component callback just as you would with any other type of function. Props are the React Function Component's parameters. Whereas the component can stay generic, we decide from the outside what it should render (or how it should behave). When rendering a component (e.g. Headline in App component), you can pass props as HTML attributes to the component.

React Function Components, This function is a valid React component because it accepts a single “props” Function and Class components both have some additional features that we will discuss in Whether you declare a component as a function or a class, it must never in response to user actions, network responses, and anything else, without  This function is a valid React component because it accepts a single “props” (which stands for properties) object argument with data and returns a React element. We call such components “function components” because they are literally JavaScript functions. You can also use an ES6 class to define a component:

Components and Props – React, Pass event handlers and other functions as props to child components: <button onClick={this.handleClick}>. If you need to have access to the parent component in the handler, you also need to Make sure you aren't calling the function when you pass it to the component: Instead, pass the function itself (without parens):. Instead of using component, use the render prop. render accepts a functional component and that function won’t get unnecessarily remounted like with component. That function will also receive all the same props that component would receive. So you can take those and pass those along to the rendered component.

Comments
  • Yea, hooks are the best bet. But you can do a function reference instead of defining the function. Something like onClick={this.handleClick}
  • Thanks! I don't have a functional component in my code, true, but I simplified this code, whereas in real life I might refactor the inner div to a functional component. However, most importantly, you confirm that this way is fine as long as you don't actually feel the pain, in terms of performance or otherwise. Helps me a lot, thanks!