Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
38
rated 0 times [  41] [ 3]  / answers: 1 / hits: 15677  / 7 Years ago, wed, august 9, 2017, 12:00:00

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!


More From » reactjs

 Answers
21

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
}

[#56822] Monday, August 7, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jeanettee

Total Points: 209
Total Questions: 97
Total Answers: 98

Location: Papua New Guinea
Member since Thu, Jul 9, 2020
4 Years ago
;