ReactJS පුලුවන් අය පොඩ්ඩක් එන්න (Help!)

Sexy Somapala

Well-known member
  • Dec 9, 2014
    2,287
    1,534
    113
    පේමන්ට් එකේ.
    මචන් මම මෙහෙම Code එක ගැහුවම, ඇයි දෙපාරක් Console Log වෙලා එන්නේ. නිකන් Response එක Console Log කරත් දෙපාරක් Console Log වෙලා එනවා.



     

    CodeMan

    Well-known member
  • Aug 26, 2021
    675
    910
    93
    Your key in the loop is changing. Every time the key changes react will see the component as different and unmounted it and remounted it.
     

    SkullVamp

    Well-known member
  • Mar 8, 2013
    783
    326
    63
    I don't know the answer to this. but i remember having a similar issue when i was learning react. I used hooks (useEffect) though. Ekenan empty array ekak pass karama it runs only once (even when state change)
     
    • Like
    Reactions: siri_ayya

    හෙළයෙක්

    Well-known member
  • Apr 26, 2014
    49,331
    100,183
    113
    component eka deparak render wenawa. component eka athule monawa hari status wenasak unoth component eka aye render wenawa.

    kohomath oya fetch wage dewal direct component athule use karana epa status management lib ekak harahama use karana
     

    Sexy Somapala

    Well-known member
  • Dec 9, 2014
    2,287
    1,534
    113
    පේමන්ට් එකේ.
    store the response in your state variable only if it is different to the original value?
    sorry. I'm at beginner level. I couldn't get what you told. but I delete the loop and only just fetched. but nothing is changed. if this is due to the key values, once I delete the loop it must be working neh?

    component eka deparak render wenawa. component eka athule monawa hari status wenasak unoth component eka aye render wenawa.

    kohomath oya fetch wage dewal direct component athule use karana epa status management lib ekak harahama use karana
    meke tutorial eke me code ekama gahala thibbe. porage eke eka parai console.log enne?

    Solved. Youtube Video එකකින් හොයාගත්තේ. මේකේ Strict Mode Tag එක අයින් කරාම එක පාරයි Console Log වෙන්නේ. තෑන්ක්ස් හැමෝටම!

    ------ Post added on Dec 24, 2022 at 1:49 PM
     

    gnilukshi

    Well-known member
  • Oct 9, 2008
    25,080
    48,517
    113
    sri lanka film GIF by Viber
     
    • Haha
    Reactions: Lokka Bro

    Anonymous_Abstract

    Well-known member
  • Aug 7, 2018
    6,036
    4,570
    113
    Messier 87

    Solved. Youtube Video එකකින් හොයාගත්තේ. මේකේ Strict Mode Tag එක අයින් කරාම එක පාරයි Console Log වෙන්නේ. තෑන්ක්ස් හැමෝටම!

    ------ Post added on Dec 24, 2022 at 1:49 PM
    :no::no: typical sri lankan developer. strict mode remove karanna pissuda ban. potential bug ekak awoth identify karanne khomada. oka ohmama thiyapan production build ekata oken affect venne na
     

    Sexy Somapala

    Well-known member
  • Dec 9, 2014
    2,287
    1,534
    113
    පේමන්ට් එකේ.
    :no::no: typical sri lankan developer. strict mode remove karanna pissuda ban. potential bug ekak awoth identify karanne khomada. oka ohmama thiyapan production build ekata oken affect venne na
    Typical Sri Lankan Developer මමද උඹද බං? බලපන් මම මෙතන ප්‍රශ්නයක් ඇහුවම උඩ ඉන්න සුපිරි කොල්ලෝ ටිකක් උන්ගේ Perspective එකෙන් බලලා උත්තරේ දීලා තියෙනවා (@CodeMan , @SkullVamp , @හෙළයෙක් , @AlphaStigma :love::love::love:). අනික මම කියලා තියෙනවා මම Beginner Level එකෙක් බුවෙක් කියලා. තාම මුල් හරියෙ ඉන්නේ. මේක Production Build එකට Affect වෙන්නේ නෑ කියන්න උඹ වගේ Senior Software Engineer (උඹේ රිප්ලයි එක දැක්කම මට එහෙම හිතුනේ :rofl::rofl::rofl:) කෙනෙක් ගෙන් අදහසක් ඕනෙ නෑ මට. ඒක මම වගේ Beginner Level එකෙක්ට තේරෙනවා. මට ඕන වුණේ Behind the scenes Code එකේ වෙන්නේ මොකක්ද කියලා දැනගන්න මිසක් උඹලා වගේ Code එකක Unexpected දෙයක් Output වුණාම Production Build එකට Effect වෙනවද නැද්ද කියලා බලලා Effect වෙන්නේ නැත්නම් Code එක Use කරන ජාතියේ දෙකයි පනහේ Software Engineer කෙනෙක් වෙන්නේ නෙවේ. උඹේ Professionalism ගතිය උඹ රිප්ලයි එක දාපු හැටියෙන පේනවා මට. ආයෙ මිනිහෙක් දෙයකට උත්තරයක් ඇහුවම තමන් දන්න දේ ලොකු කරලා පෙන්නන්නේ නැතිව නිහතමානීව උත්තරයක් දීපන්. Coding වලට කලින් ඉගෙනගන්න දේවල් ගොඩක් උඹට තියෙනවා. යට පොටෝ එකයි උඹේ Professionalism ගතියෙයි කිසිම සම්බන්ධයක් නෑලු ඉතින්.

     

    CodeMan

    Well-known member
  • Aug 26, 2021
    675
    910
    93
    @Sexy Somapala Machan, rendering two times is actually not a bug. It's a feature. So you need to design your code in a way that it is not a problem for the component to unmounted and then mounted again. I took your code and change a bit it as follows. Though the render happens two times the list only shows up when the response gets back with data.

    It is good you asked this question. I was also not 100% sure what's going on so had to dig in.

    import React, { Component } from 'react';
    class App extends Component{
    constructor(){
    super()
    this.state = {
    monsters: []
    }
    }
    componentDidMount() {
    console.log("inside componentDidMount");
    fetch('https://jsonplaceholder.typicode.com/users')
    .then((response) => response.json())
    .then((users) => {
    this.setState({monsters: users})
    console.log(this.state.monsters)
    })
    }
    render(){
    return(
    <>
    { this.state.monsters.map((monster) => {
    <h1 key={monster.id}>{ monster.name }</h1>
    })}
    </>

    )
    }
    }
    export default App;
     

    Tharaka25

    Well-known member
  • Feb 12, 2018
    2,620
    2,232
    113
    Matath ohoma vela strict mode ayn kalama hari giya. Rendering bugs ehema hoya ganna lesi vennalu ehema thiyenne