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!
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”}.
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.
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.
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.
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:
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With