React useEffect() not executing before page render

I’m trying to pull postcode information and display it on my react app. The code so far looks as such:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const UKPar = () => {
postcode = "SW1A2AA"
const [ukPostcodeData, setUKPostcodeData] = useState({});
useEffect(() => {
fetch('https://api.postcodes.io/postcodes/' + postcode)
.then((res) => res.json())
.then((data) => {
console.log(data);
setUKPostcodeData(data);
})
.catch((err) => {
console.log(err.message);
});
}, [postcode]);
return(...)
}
export default UKPar;
</code>
<code>const UKPar = () => { postcode = "SW1A2AA" const [ukPostcodeData, setUKPostcodeData] = useState({}); useEffect(() => { fetch('https://api.postcodes.io/postcodes/' + postcode) .then((res) => res.json()) .then((data) => { console.log(data); setUKPostcodeData(data); }) .catch((err) => { console.log(err.message); }); }, [postcode]); return(...) } export default UKPar; </code>
const UKPar = () => {

    postcode = "SW1A2AA"
    const [ukPostcodeData, setUKPostcodeData] = useState({});

    useEffect(() => {
        fetch('https://api.postcodes.io/postcodes/' + postcode)
           .then((res) => res.json())
           .then((data) => {
              console.log(data);
              setUKPostcodeData(data);
           })
           .catch((err) => {
              console.log(err.message);
           });
    }, [postcode]);

    return(...)
}
export default UKPar;

This works fine and I can see that the data is displayed in the browser log as so:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"status": 200,
"result": {
"postcode": "SW1A 2AA",
"quality": 1,
"eastings": 530047,
"northings": 179951,
"country": "England",
"nhs_ha": "London",
"longitude": -0.127695,
"latitude": 51.50354,
"european_electoral_region": "London",
"primary_care_trust": "Westminster",
"region": "London",
"lsoa": "Westminster 018C",
"msoa": "Westminster 018",
"incode": "2AA",
"outcode": "SW1A",
"parliamentary_constituency": "Cities of London and Westminster",
"parliamentary_constituency_2024": "Cities of London and Westminster",
"admin_district": "Westminster",
"parish": "Westminster, unparished area",
"admin_county": null,
"date_of_introduction": "198001",
"admin_ward": "St James's",
"ced": null,
"ccg": "NHS North West London",
"nuts": "Westminster",
"pfa": "Metropolitan Police",
"codes": {
"admin_district": "E09000033",
"admin_county": "E99999999",
"admin_ward": "E05013806",
"parish": "E43000236",
"parliamentary_constituency": "E14000639",
"parliamentary_constituency_2024": "E14001172",
"ccg": "E38000256",
"ccg_id": "W2U3Z",
"ced": "E99999999",
"nuts": "TLI32",
"lsoa": "E01004736",
"msoa": "E02000977",
"lau2": "E09000033",
"pfa": "E23000001"
}
}
}
</code>
<code>{ "status": 200, "result": { "postcode": "SW1A 2AA", "quality": 1, "eastings": 530047, "northings": 179951, "country": "England", "nhs_ha": "London", "longitude": -0.127695, "latitude": 51.50354, "european_electoral_region": "London", "primary_care_trust": "Westminster", "region": "London", "lsoa": "Westminster 018C", "msoa": "Westminster 018", "incode": "2AA", "outcode": "SW1A", "parliamentary_constituency": "Cities of London and Westminster", "parliamentary_constituency_2024": "Cities of London and Westminster", "admin_district": "Westminster", "parish": "Westminster, unparished area", "admin_county": null, "date_of_introduction": "198001", "admin_ward": "St James's", "ced": null, "ccg": "NHS North West London", "nuts": "Westminster", "pfa": "Metropolitan Police", "codes": { "admin_district": "E09000033", "admin_county": "E99999999", "admin_ward": "E05013806", "parish": "E43000236", "parliamentary_constituency": "E14000639", "parliamentary_constituency_2024": "E14001172", "ccg": "E38000256", "ccg_id": "W2U3Z", "ced": "E99999999", "nuts": "TLI32", "lsoa": "E01004736", "msoa": "E02000977", "lau2": "E09000033", "pfa": "E23000001" } } } </code>
{
    "status": 200,
    "result": {
        "postcode": "SW1A 2AA",
        "quality": 1,
        "eastings": 530047,
        "northings": 179951,
        "country": "England",
        "nhs_ha": "London",
        "longitude": -0.127695,
        "latitude": 51.50354,
        "european_electoral_region": "London",
        "primary_care_trust": "Westminster",
        "region": "London",
        "lsoa": "Westminster 018C",
        "msoa": "Westminster 018",
        "incode": "2AA",
        "outcode": "SW1A",
        "parliamentary_constituency": "Cities of London and Westminster",
        "parliamentary_constituency_2024": "Cities of London and Westminster",
        "admin_district": "Westminster",
        "parish": "Westminster, unparished area",
        "admin_county": null,
        "date_of_introduction": "198001",
        "admin_ward": "St James's",
        "ced": null,
        "ccg": "NHS North West London",
        "nuts": "Westminster",
        "pfa": "Metropolitan Police",
        "codes": {
            "admin_district": "E09000033",
            "admin_county": "E99999999",
            "admin_ward": "E05013806",
            "parish": "E43000236",
            "parliamentary_constituency": "E14000639",
            "parliamentary_constituency_2024": "E14001172",
            "ccg": "E38000256",
            "ccg_id": "W2U3Z",
            "ced": "E99999999",
            "nuts": "TLI32",
            "lsoa": "E01004736",
            "msoa": "E02000977",
            "lau2": "E09000033",
            "pfa": "E23000001"
        }
    }
}

The issue is when I try and display this information the browser throws an error saying “ukPostcodeData.result is undefined”. I have have used the following code to try and display the information I need and other testing has shown that this should give me the correct result, however is causing errors when the HTML is rendered.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><td>{ukPostcodeData.result.parliamentary_constituency}</td>
</code>
<code><td>{ukPostcodeData.result.parliamentary_constituency}</td> </code>
<td>{ukPostcodeData.result.parliamentary_constituency}</td>

I have tried using other methods to call the information needed from ukPostcodeData to see if this would fix the problem. I used:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const getConstituency = () => {
return ukPostcodeData.result.parliamentary_constituency
}
...
return (
...
<td>{getConstituency()}</td>
...
)
</code>
<code>const getConstituency = () => { return ukPostcodeData.result.parliamentary_constituency } ... return ( ... <td>{getConstituency()}</td> ... ) </code>
const getConstituency = () => {
    return ukPostcodeData.result.parliamentary_constituency
}

...
return (
...
    <td>{getConstituency()}</td>
...
)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật