2

I want to render the columns dynamically which are given inside the childeren of the ParentComponent as CustomColumns like this:

import { Column } from 'primereact/column';
import { DataTable } from 'primereact/datatable';

function TetsPage(): JSX.Element {
    return (
        <ParentComponent>
            <CustomColumn field="name" header="Name" />
        </ParentComponent>
    );
}

export class ParentComponent extends React.Component<Props, State> {
    ................................
    render(): JSX.Element {        
        // result:   <Column field="name" header="Name" />
        // expected: <Column field="name" header="Name" sortable={true}/>
        return (
            <DataTable value={this.state.list}>
                {this.props.children}
            </DataTable>
        );
     }
}


export class CustomColumn extends React.Component<Props, State> {
    .................................
    render(): JSX.Element {
        return (
            <Column field={this.props.field} header={this.props.header} sortable={true} />
        )
    }
}

The problem is the DataTable component renders its own Column components instead of my CustomColumn which is customized/extended version of the Column component.

How can I get my CustomColumns rendered as dynamic columns inside the DataTable?

Here is the CodeSandbox

7
  • can you add this code in codesandbox or jsfiddle ? Commented Nov 4, 2019 at 10:02
  • @NihalSaxena I've added the link for CodeSandbox;) Commented Nov 4, 2019 at 11:37
  • what is the customised part in CustomColumn ? Commented Nov 4, 2019 at 11:53
  • 1
    check this: codesandbox.io/s/stoic-chaplygin-ky4dt Commented Nov 4, 2019 at 12:19
  • 1
    @NihalSaxena I see what you try to do but the purpose of the CustomColumn component is to reduce the complexity during defining the columns. sortable is just an example. Imagine that there will be also filters, formatters... based on conditions. Thank you anyway. Commented Nov 5, 2019 at 7:11

1 Answer 1

3
+100

Please find the updated sandbox for the same https://codesandbox.io/s/loving-snow-qlp8c

It's actually a very good question! After lot of headbanging, I found the solution. Actually, the problem is not the way you wrote the react component, it is the problem with the Column of DataTable. If you look in their GITHUB code, you will find it is just a Component with defaultProps. You just need to pass sortable as a defaultProps to your CustomColumn component and TADA! It should work.

I would like to add why it was working with the props for field and header, it's because when the Column was instantiated the field and header was instantiated with props so defaultProps doesn't come to picture. But when you were passing sortable from CustomColumn there was no way for React to understand it has to re-instantiate the props again as Component is already instantiated and there is no change for the props as the Column component is just a Component with defaultProps. It is very lame for primereact folks to design components in this manner. You can raise an issue in their GITHUB as well

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.