Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass props to react component that wrapped in variable

I want to render table cell dynamically. Each cell is a React component. I'm trying to export these React components as a wrap function.

For example:

import cellA from './cellA'
import cellB from './cellB'
import cellC from './cellC'

let content = { cellA, cellB, cellC }

function tableize (a) {
    let resultFn = {}
    Object.keys(a).forEach((k) => {
        let element = a[k]
        resultFn[k] = function (data) {
            return (<element data={data} />)
        }
    })
    return resultFn
}

export default tableize(content)

The problem is on this line:

return (<element data={data} />)

The result is browser render list of React components named element, not cellA, cellB, cellC. The function return element as jsx (in < /> tag) because I need to pass props to these React component. But I'm wrong.

How to pass props to this React component that wrapped in a variable?

Thank you!

like image 611
Andarias Silvanus Avatar asked Aug 09 '17 05:08

Andarias Silvanus


People also ask

How do you pass props to Reactjs component that wrapped in variable?

cloneElement() method used here. This method is used to create the clone of the element that will be given as an argument, also here we can pass props and children as arguments. Here variable “c” is passed as an element in React. cloneElement() method for cloning, also passed props {name1: “C++”, name2: “JAVA”}.

How do you pass non string props in React?

To pass a number as props to a component in React, wrap the number in curly braces, e.g. <Child num={42} /> . All props you pass to a component that are not of type string have to be wrapped in curly braces.

How do you pass props in React components?

export default App; Basically that's how props are passed from component to component in React. As you may have noticed, props are only passed from top to bottom in React application's component hierarchy. There is no way to pass props up to a parent component from a child component.

How do props work in react?

Understanding the way props work is essential to mastering React, but to grasp the concept fully is no easy thing. Props stands for “properties,” and they are used in a React application to send data from one React component to another React component. Let’s take a look at the example code below.

What is proptypes in ReactJS?

PropTypes is a special component property that can be used to validate the props you have in a component. It’s a separate, optional npm package, so you need to install it first before using it:

What is defaultprops in react greeting?

And now the default values in defaultProps will be used when Greeting is called without props. Learn more about default props in React here. A parent component is any component that calls other components in its code block, while a child component is simply a component that gets called by a parent component.

How do you make a wrapper component in React React?

React gives you a built-in prop called children that collects any children components. Using this makes creating wrapper components intuitivie and readable. To start, make a new component called Card. This will be a wrapper component to create a standard style for any new card components.


1 Answers

Try this:

function tableize (a) {
    let resultFn = {}
    Object.keys(a).forEach((k) => {
        // Uppercase "E" from Element
        let Element = a[k]
        resultFn[k] = function (data) {
            // Uppercase "E" from Element
            return (<Element data={data} />)
        }
    })
    return resultFn
}
like image 88
Win Avatar answered Oct 19 '22 02:10

Win