I've been coding a columns component which creates a base div, and inside, you place every children you want. The component takes every single child inside the columns div and place them in another div. No need to create columns inside. The problem is that once I go through all the children and create the element, the functions returns me undefined.
React code
import React from 'react';
import './columns.css';
export const Columns = ({
children,
columnsClass
}) => {
const checkClasses = () => {
if(columnsClass){ return columnsClass; }
}
const getChildren = () => {
children.map((child, index) => {
return React.createElement(child.type, {key: index, className: `app-column app-column-${index = index + 1}`}, child.props.children);
})
}
return (<div className={`app-columns ${checkClasses()}`}>{getChildren()}</div>);
}
Children console.log
Object { "$$typeof": Symbol("react.element"), type: "div", key: null, ref: null, props: {…},
_owner: {…}, _store: {…}, … }
"$$typeof": Symbol("react.element")
_owner: Object { tag: 0, key: null, index: 0, … }
_self: undefined
_source: Object { fileName: "C:\\wamp64\\www\\Proyectos\\Portfolio\\src\\routes\\home.js", lineNumber: 52, columnNumber: 17 }
_store: Object { … }
key: null
props: Object { children: "Columna 5" }
ref: null
type: "div"
Updated code | Solution
import React from 'react';
import './columns.css';
export const Columns = ({
children,
columnsClass
}) => {
const checkClasses = () => {
if(columnsClass){ return columnsClass; }
}
return (<div className={`app-columns ${checkClasses()}`}>
{
children.map((child, index) => {
return React.createElement(child.type, {key: index, className: `app-column app-column-${index = index + 1}`}, child.props.children);
})
}
</div>);
}