currentRecordId

How to get current recordId in LWC

LWC

By using force:hasRecordId interface in Aura component we can get the id of the current record however in LWC it is very easy to get the id of the current record.

In the component’s JavaScript class, we use @api decorator to create a public recordId property as shown below.

fetchRecordId.js

import { LightningElement, api } from 'lwc';

export default class FetchRecordId extends LightningElement {

    @api recordId;

}

fetchRecordId.html

<template>
    This is curent recordId:- {recordId}
</template>

fetchRecordId.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

    <apiVersion>50.0</apiVersion>

    <isExposed>true</isExposed>

    <targets>

        <target>lightning__RecordPage</target>

    </targets>

</LightningComponentBundle>

if we drag this component on lightning record Page then we can see current record Id on this component

Leave a Reply