Methods inside of functional components












0















I'm experiencing a strange issue where the onPress function of a functional child component is triggered whenever the component is rendered.



Relevant parent code which renders the child component from a list of dates. I was also having issues with array.map if the component was a class component, but that's a separate issue.



return list_items.map(data => 
<ChildComponent
key={data.day}
props={data}
/>);


And the child component:



const ChildComponent = ({ props }) => {
let date = props.date;
const someFunction = () => {
console.log(date)
}
return (
<Text><Icon name="angle-right" onPress={someFunction()}/></Text>
)


And what I get from the console upon rendering the relevant scene:




2018-11-11



2018-11-12



2018-11-13



2018-11-14



2018-11-15



2018-11-16




Pressing the icon does nothing, yet it seems to run the function as if it was a lifecycle component. I've also moved the function into the parent component and passed it to the child as recommended per Guruparan Giritharan, and it still executes on render.



return list_items.map(data => 
<ChildComponent
key={data.day}
props={data}
someFunction={this.someFunction} // function now within parent component
/>);


And the updated child:



const ChildComponent = ({ props, someFunction }) => {
return (
<Text>
<Icon name="angle-right" onPress={someFunction(props.date)}/>
</Text>
)
}









share|improve this question





























    0















    I'm experiencing a strange issue where the onPress function of a functional child component is triggered whenever the component is rendered.



    Relevant parent code which renders the child component from a list of dates. I was also having issues with array.map if the component was a class component, but that's a separate issue.



    return list_items.map(data => 
    <ChildComponent
    key={data.day}
    props={data}
    />);


    And the child component:



    const ChildComponent = ({ props }) => {
    let date = props.date;
    const someFunction = () => {
    console.log(date)
    }
    return (
    <Text><Icon name="angle-right" onPress={someFunction()}/></Text>
    )


    And what I get from the console upon rendering the relevant scene:




    2018-11-11



    2018-11-12



    2018-11-13



    2018-11-14



    2018-11-15



    2018-11-16




    Pressing the icon does nothing, yet it seems to run the function as if it was a lifecycle component. I've also moved the function into the parent component and passed it to the child as recommended per Guruparan Giritharan, and it still executes on render.



    return list_items.map(data => 
    <ChildComponent
    key={data.day}
    props={data}
    someFunction={this.someFunction} // function now within parent component
    />);


    And the updated child:



    const ChildComponent = ({ props, someFunction }) => {
    return (
    <Text>
    <Icon name="angle-right" onPress={someFunction(props.date)}/>
    </Text>
    )
    }









    share|improve this question



























      0












      0








      0








      I'm experiencing a strange issue where the onPress function of a functional child component is triggered whenever the component is rendered.



      Relevant parent code which renders the child component from a list of dates. I was also having issues with array.map if the component was a class component, but that's a separate issue.



      return list_items.map(data => 
      <ChildComponent
      key={data.day}
      props={data}
      />);


      And the child component:



      const ChildComponent = ({ props }) => {
      let date = props.date;
      const someFunction = () => {
      console.log(date)
      }
      return (
      <Text><Icon name="angle-right" onPress={someFunction()}/></Text>
      )


      And what I get from the console upon rendering the relevant scene:




      2018-11-11



      2018-11-12



      2018-11-13



      2018-11-14



      2018-11-15



      2018-11-16




      Pressing the icon does nothing, yet it seems to run the function as if it was a lifecycle component. I've also moved the function into the parent component and passed it to the child as recommended per Guruparan Giritharan, and it still executes on render.



      return list_items.map(data => 
      <ChildComponent
      key={data.day}
      props={data}
      someFunction={this.someFunction} // function now within parent component
      />);


      And the updated child:



      const ChildComponent = ({ props, someFunction }) => {
      return (
      <Text>
      <Icon name="angle-right" onPress={someFunction(props.date)}/>
      </Text>
      )
      }









      share|improve this question
















      I'm experiencing a strange issue where the onPress function of a functional child component is triggered whenever the component is rendered.



      Relevant parent code which renders the child component from a list of dates. I was also having issues with array.map if the component was a class component, but that's a separate issue.



      return list_items.map(data => 
      <ChildComponent
      key={data.day}
      props={data}
      />);


      And the child component:



      const ChildComponent = ({ props }) => {
      let date = props.date;
      const someFunction = () => {
      console.log(date)
      }
      return (
      <Text><Icon name="angle-right" onPress={someFunction()}/></Text>
      )


      And what I get from the console upon rendering the relevant scene:




      2018-11-11



      2018-11-12



      2018-11-13



      2018-11-14



      2018-11-15



      2018-11-16




      Pressing the icon does nothing, yet it seems to run the function as if it was a lifecycle component. I've also moved the function into the parent component and passed it to the child as recommended per Guruparan Giritharan, and it still executes on render.



      return list_items.map(data => 
      <ChildComponent
      key={data.day}
      props={data}
      someFunction={this.someFunction} // function now within parent component
      />);


      And the updated child:



      const ChildComponent = ({ props, someFunction }) => {
      return (
      <Text>
      <Icon name="angle-right" onPress={someFunction(props.date)}/>
      </Text>
      )
      }






      react-native jsx






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 16:50







      NathanL

















      asked Nov 13 '18 at 5:03









      NathanLNathanL

      769




      769
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Change to



          <Text><Icon name="angle-right" onPress={someFunction}/></Text>


          You are calling the function there itself instead of passing the reference. So the function is called inside each render instead of onPress.






          share|improve this answer
























          • I changed it so that the function exists inside of the parent component and is being called by the child component, yet no change.

            – NathanL
            Nov 13 '18 at 16:44






          • 1





            Do you pass the function of parent component as a prop ?, do you get the console log when you press the icon ?

            – Guruparan Giritharan
            Nov 13 '18 at 16:49






          • 1





            you are calling the function on render onPress={someFunction(props.date)} thats the problem it should be a reference onPress={this.props.someFunction}

            – Guruparan Giritharan
            Nov 13 '18 at 17:00






          • 1





            if you need to use someFunction in your onPress along with date the way will be <Icon name="angle-right" onPress={()=>someFunction(props.date)}/>

            – Guruparan Giritharan
            Nov 13 '18 at 17:11






          • 1





            I changed it back to a class-based component and it worked.

            – NathanL
            Nov 13 '18 at 17:21











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274126%2fmethods-inside-of-functional-components%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Change to



          <Text><Icon name="angle-right" onPress={someFunction}/></Text>


          You are calling the function there itself instead of passing the reference. So the function is called inside each render instead of onPress.






          share|improve this answer
























          • I changed it so that the function exists inside of the parent component and is being called by the child component, yet no change.

            – NathanL
            Nov 13 '18 at 16:44






          • 1





            Do you pass the function of parent component as a prop ?, do you get the console log when you press the icon ?

            – Guruparan Giritharan
            Nov 13 '18 at 16:49






          • 1





            you are calling the function on render onPress={someFunction(props.date)} thats the problem it should be a reference onPress={this.props.someFunction}

            – Guruparan Giritharan
            Nov 13 '18 at 17:00






          • 1





            if you need to use someFunction in your onPress along with date the way will be <Icon name="angle-right" onPress={()=>someFunction(props.date)}/>

            – Guruparan Giritharan
            Nov 13 '18 at 17:11






          • 1





            I changed it back to a class-based component and it worked.

            – NathanL
            Nov 13 '18 at 17:21
















          1














          Change to



          <Text><Icon name="angle-right" onPress={someFunction}/></Text>


          You are calling the function there itself instead of passing the reference. So the function is called inside each render instead of onPress.






          share|improve this answer
























          • I changed it so that the function exists inside of the parent component and is being called by the child component, yet no change.

            – NathanL
            Nov 13 '18 at 16:44






          • 1





            Do you pass the function of parent component as a prop ?, do you get the console log when you press the icon ?

            – Guruparan Giritharan
            Nov 13 '18 at 16:49






          • 1





            you are calling the function on render onPress={someFunction(props.date)} thats the problem it should be a reference onPress={this.props.someFunction}

            – Guruparan Giritharan
            Nov 13 '18 at 17:00






          • 1





            if you need to use someFunction in your onPress along with date the way will be <Icon name="angle-right" onPress={()=>someFunction(props.date)}/>

            – Guruparan Giritharan
            Nov 13 '18 at 17:11






          • 1





            I changed it back to a class-based component and it worked.

            – NathanL
            Nov 13 '18 at 17:21














          1












          1








          1







          Change to



          <Text><Icon name="angle-right" onPress={someFunction}/></Text>


          You are calling the function there itself instead of passing the reference. So the function is called inside each render instead of onPress.






          share|improve this answer













          Change to



          <Text><Icon name="angle-right" onPress={someFunction}/></Text>


          You are calling the function there itself instead of passing the reference. So the function is called inside each render instead of onPress.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 6:32









          Guruparan GiritharanGuruparan Giritharan

          4651618




          4651618













          • I changed it so that the function exists inside of the parent component and is being called by the child component, yet no change.

            – NathanL
            Nov 13 '18 at 16:44






          • 1





            Do you pass the function of parent component as a prop ?, do you get the console log when you press the icon ?

            – Guruparan Giritharan
            Nov 13 '18 at 16:49






          • 1





            you are calling the function on render onPress={someFunction(props.date)} thats the problem it should be a reference onPress={this.props.someFunction}

            – Guruparan Giritharan
            Nov 13 '18 at 17:00






          • 1





            if you need to use someFunction in your onPress along with date the way will be <Icon name="angle-right" onPress={()=>someFunction(props.date)}/>

            – Guruparan Giritharan
            Nov 13 '18 at 17:11






          • 1





            I changed it back to a class-based component and it worked.

            – NathanL
            Nov 13 '18 at 17:21



















          • I changed it so that the function exists inside of the parent component and is being called by the child component, yet no change.

            – NathanL
            Nov 13 '18 at 16:44






          • 1





            Do you pass the function of parent component as a prop ?, do you get the console log when you press the icon ?

            – Guruparan Giritharan
            Nov 13 '18 at 16:49






          • 1





            you are calling the function on render onPress={someFunction(props.date)} thats the problem it should be a reference onPress={this.props.someFunction}

            – Guruparan Giritharan
            Nov 13 '18 at 17:00






          • 1





            if you need to use someFunction in your onPress along with date the way will be <Icon name="angle-right" onPress={()=>someFunction(props.date)}/>

            – Guruparan Giritharan
            Nov 13 '18 at 17:11






          • 1





            I changed it back to a class-based component and it worked.

            – NathanL
            Nov 13 '18 at 17:21

















          I changed it so that the function exists inside of the parent component and is being called by the child component, yet no change.

          – NathanL
          Nov 13 '18 at 16:44





          I changed it so that the function exists inside of the parent component and is being called by the child component, yet no change.

          – NathanL
          Nov 13 '18 at 16:44




          1




          1





          Do you pass the function of parent component as a prop ?, do you get the console log when you press the icon ?

          – Guruparan Giritharan
          Nov 13 '18 at 16:49





          Do you pass the function of parent component as a prop ?, do you get the console log when you press the icon ?

          – Guruparan Giritharan
          Nov 13 '18 at 16:49




          1




          1





          you are calling the function on render onPress={someFunction(props.date)} thats the problem it should be a reference onPress={this.props.someFunction}

          – Guruparan Giritharan
          Nov 13 '18 at 17:00





          you are calling the function on render onPress={someFunction(props.date)} thats the problem it should be a reference onPress={this.props.someFunction}

          – Guruparan Giritharan
          Nov 13 '18 at 17:00




          1




          1





          if you need to use someFunction in your onPress along with date the way will be <Icon name="angle-right" onPress={()=>someFunction(props.date)}/>

          – Guruparan Giritharan
          Nov 13 '18 at 17:11





          if you need to use someFunction in your onPress along with date the way will be <Icon name="angle-right" onPress={()=>someFunction(props.date)}/>

          – Guruparan Giritharan
          Nov 13 '18 at 17:11




          1




          1





          I changed it back to a class-based component and it worked.

          – NathanL
          Nov 13 '18 at 17:21





          I changed it back to a class-based component and it worked.

          – NathanL
          Nov 13 '18 at 17:21


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274126%2fmethods-inside-of-functional-components%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          Bicuculline

          さくらももこ