0

In a class component(a typescript file), I have the actionTemplate defined in the following manner:

//Inside constructor: 

constructor(props: Props) {
        super(props);
        this.actionTemplate = this.actionTemplate.bind(this);
      

        this.state = {
           //state variables
        };
    }

actionTemplate = (rowData: any) => (
        <div style={{textAlign: 'center', width: '6em'}}>
            <span>
                <Button type='button' icon="pi pi-pencil" style={{marginRight: '5px'}} onClick={(e) => this.handleClick(rowData, e)} tooltip='Edit'/>
                <Button icon="pi pi-trash" style={{marginRight: '5px'}} tooltip='Delete' />
                {
                  rowData.fullPathName !== null &&
                  <Button icon="pi pi-download" tooltip='Download' onClick={(e) => this.handleDownloadClick(rowData, e)} />
                }
            </span>
        </div>
    );  

const GridView = labelValue => ((this.state.assocVisible || this.state.assetFormVisible)? null: (
            <div>
                <DataTable
                   //some datable properties
                >
                    <Column key='actionButton' field='actionButton' header='Action' body={this.actionTemplate} style={{maxHeight: '30px !important', whiteSpace: 'nowrap', overflow: 'hidden'}}/>
                    {dynamicColumns}
                </DataTable>
               
            </div>
        ))

However, I am using the same thing in a functional component without a class and I did the following ( as suggested in this thread - using arrow function):

actionTemplate = (rowData) => (
    <div style={{textAlign: 'center', width: '6em'}}>
        <span>
            <Button type='button' icon="pi pi-pencil" style={{marginRight: '5px'}} onClick={(e) => handleClick(rowData, e)} tooltip='Edit'/>
            <Button icon="pi pi-trash" style={{marginRight: '5px'}} tooltip='Delete' />
            {
              rowData.fullPathName !== null &&
              <Button icon="pi pi-download" tooltip='Download' onClick={(e) => handleDownloadClick(rowData, e)} />
            }
        </span>
    </div>
);

const GridView = labelValue => ((assocVisible[0] || assetFormVisible[0])? null: (
        <div>
            <DataTable
                //some datable properties 
            >
                <Column key='actionButton' field='actionButton' header='Action' body={actionTemplate} style={{maxHeight: '30px !important', whiteSpace: 'nowrap', overflow: 'hidden'}}/>
                {dynamicColumns}
            </DataTable>
            {/* {assocButtonView(labelValue)} */}
        </div>
    ))

And it keeps on throwing following error:

Uncaught ReferenceError: assignment to undeclared variable actionTemplate

When I added var in front of actionTemplate, it didn't throw any error, but I don't see any buttons in my Action column. What am I missing ?

1 Answer 1

1

In functional component, you are not declaring another function, you have to assign the function to a variable.

So this is the correct syntax:

const actionTemplate = (rowData) => (
    <div style={{textAlign: 'center', width: '6em'}}>
        <span>
            <Button type='button' icon="pi pi-pencil" style={{marginRight: '5px'}} onClick={(e) => handleClick(rowData, e)} tooltip='Edit'/>
            <Button icon="pi pi-trash" style={{marginRight: '5px'}} tooltip='Delete' />
            {
              rowData.fullPathName !== null &&
              <Button icon="pi pi-download" tooltip='Download' onClick={(e) => handleDownloadClick(rowData, e)} />
            }
        </span>
    </div>
);

const GridView = labelValue => ((assocVisible[0] || assetFormVisible[0])? null: (
        <div>
            <DataTable
                //some datable properties 
            >
                <Column key='actionButton' field='actionButton' header='Action' body={actionTemplate} style={{maxHeight: '30px !important', whiteSpace: 'nowrap', overflow: 'hidden'}}/>
                {dynamicColumns}
            </DataTable>
            {/* {assocButtonView(labelValue)} */}
        </div>
    ))
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. This doesn't throw any error. However, the buttons are still missing from the Action column. Any idea why?
I am wondering if I am calling the actionTemplate properly? Like in class component, it gets called like this body={this.actionTemplate} and in functional component, I am doing it like this ` body={actionTemplate}`
can you share the codeSandbox? I'm not able to debug that for you otherwise

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.