How to make a parameterised function call in react native nav header











up vote
2
down vote

favorite












//I am trying to call a parameterized function in a header but could not as I //am unable to find to way to pass parameter.



class MyScreen extends React.Component {

static navigationOptions = ({ navigation }) =>
{
headerLeft: (
<SearchBar
placeholder="Search"
round
onChangeText={text => this.searchFunction(text)}
/>
)
};

*searchFunction(text)
{
alert( text + ' searched succesfully');
}*

componentDidMount()
{
**//I would need implementation here**
}

render()
{
return (<View />);
}

}








share









New contributor




Umar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    2
    down vote

    favorite












    //I am trying to call a parameterized function in a header but could not as I //am unable to find to way to pass parameter.



    class MyScreen extends React.Component {

    static navigationOptions = ({ navigation }) =>
    {
    headerLeft: (
    <SearchBar
    placeholder="Search"
    round
    onChangeText={text => this.searchFunction(text)}
    />
    )
    };

    *searchFunction(text)
    {
    alert( text + ' searched succesfully');
    }*

    componentDidMount()
    {
    **//I would need implementation here**
    }

    render()
    {
    return (<View />);
    }

    }








    share









    New contributor




    Umar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      //I am trying to call a parameterized function in a header but could not as I //am unable to find to way to pass parameter.



      class MyScreen extends React.Component {

      static navigationOptions = ({ navigation }) =>
      {
      headerLeft: (
      <SearchBar
      placeholder="Search"
      round
      onChangeText={text => this.searchFunction(text)}
      />
      )
      };

      *searchFunction(text)
      {
      alert( text + ' searched succesfully');
      }*

      componentDidMount()
      {
      **//I would need implementation here**
      }

      render()
      {
      return (<View />);
      }

      }








      share









      New contributor




      Umar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      //I am trying to call a parameterized function in a header but could not as I //am unable to find to way to pass parameter.



      class MyScreen extends React.Component {

      static navigationOptions = ({ navigation }) =>
      {
      headerLeft: (
      <SearchBar
      placeholder="Search"
      round
      onChangeText={text => this.searchFunction(text)}
      />
      )
      };

      *searchFunction(text)
      {
      alert( text + ' searched succesfully');
      }*

      componentDidMount()
      {
      **//I would need implementation here**
      }

      render()
      {
      return (<View />);
      }

      }






      android





      share









      New contributor




      Umar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share









      New contributor




      Umar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share








      edited yesterday









      Poul Bak

      5,0003732




      5,0003732






      New contributor




      Umar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked yesterday









      Umar

      111




      111




      New contributor




      Umar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Umar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Umar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          The reserved word this means nothing in the static context of the navigationOptions function, So you can't use it there to call the searchFunction.



          There's a way to add params to the navigation object so you can get them in the navigationOptions static function.



          You can add the searchFunction as a navigation object param and pass it to the onChangeText attribute.



          The implementation looks like this:



          class MyScreen extends React.Component {

          // Pass the searchFunction from the navigation params to the `onChangeText` attribute.
          // It should be triggered with the `text` argument.
          static navigationOptions = ({ navigation }) =>
          {
          headerLeft: (
          <SearchBar
          placeholder="Search"
          round
          onChangeText={navigation.getParam('searchFunc')}
          />
          )
          };

          // Use arrow function to bind it to the MyScreen class.
          // (I'm not sure you have to do it like this, try to use it as a normal function first)
          searchFunction = (text) => {
          alert( text + ' searched succesfully');
          }

          // Add the `searchFunction` as a navigation param:
          componentDidMount() {
          this.props.navigation.setParams({searchFunc: this.searchFunction})
          }

          // Since we pass a class function as a param
          // I believe it would be a good practice to remove it
          // from the navigation params when the Component unmounts.
          componentWillUnmount() {
          this.props.navigation.setParams({searchFunc: null})
          }

          render() {
          return (<View />);
          }
          }


          Source






          share|improve this answer























          • I will give it a try. Thanks for the answer.
            – Umar
            1 hour ago











          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',
          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
          });


          }
          });






          Umar is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53346613%2fhow-to-make-a-parameterised-function-call-in-react-native-nav-header%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








          up vote
          0
          down vote













          The reserved word this means nothing in the static context of the navigationOptions function, So you can't use it there to call the searchFunction.



          There's a way to add params to the navigation object so you can get them in the navigationOptions static function.



          You can add the searchFunction as a navigation object param and pass it to the onChangeText attribute.



          The implementation looks like this:



          class MyScreen extends React.Component {

          // Pass the searchFunction from the navigation params to the `onChangeText` attribute.
          // It should be triggered with the `text` argument.
          static navigationOptions = ({ navigation }) =>
          {
          headerLeft: (
          <SearchBar
          placeholder="Search"
          round
          onChangeText={navigation.getParam('searchFunc')}
          />
          )
          };

          // Use arrow function to bind it to the MyScreen class.
          // (I'm not sure you have to do it like this, try to use it as a normal function first)
          searchFunction = (text) => {
          alert( text + ' searched succesfully');
          }

          // Add the `searchFunction` as a navigation param:
          componentDidMount() {
          this.props.navigation.setParams({searchFunc: this.searchFunction})
          }

          // Since we pass a class function as a param
          // I believe it would be a good practice to remove it
          // from the navigation params when the Component unmounts.
          componentWillUnmount() {
          this.props.navigation.setParams({searchFunc: null})
          }

          render() {
          return (<View />);
          }
          }


          Source






          share|improve this answer























          • I will give it a try. Thanks for the answer.
            – Umar
            1 hour ago















          up vote
          0
          down vote













          The reserved word this means nothing in the static context of the navigationOptions function, So you can't use it there to call the searchFunction.



          There's a way to add params to the navigation object so you can get them in the navigationOptions static function.



          You can add the searchFunction as a navigation object param and pass it to the onChangeText attribute.



          The implementation looks like this:



          class MyScreen extends React.Component {

          // Pass the searchFunction from the navigation params to the `onChangeText` attribute.
          // It should be triggered with the `text` argument.
          static navigationOptions = ({ navigation }) =>
          {
          headerLeft: (
          <SearchBar
          placeholder="Search"
          round
          onChangeText={navigation.getParam('searchFunc')}
          />
          )
          };

          // Use arrow function to bind it to the MyScreen class.
          // (I'm not sure you have to do it like this, try to use it as a normal function first)
          searchFunction = (text) => {
          alert( text + ' searched succesfully');
          }

          // Add the `searchFunction` as a navigation param:
          componentDidMount() {
          this.props.navigation.setParams({searchFunc: this.searchFunction})
          }

          // Since we pass a class function as a param
          // I believe it would be a good practice to remove it
          // from the navigation params when the Component unmounts.
          componentWillUnmount() {
          this.props.navigation.setParams({searchFunc: null})
          }

          render() {
          return (<View />);
          }
          }


          Source






          share|improve this answer























          • I will give it a try. Thanks for the answer.
            – Umar
            1 hour ago













          up vote
          0
          down vote










          up vote
          0
          down vote









          The reserved word this means nothing in the static context of the navigationOptions function, So you can't use it there to call the searchFunction.



          There's a way to add params to the navigation object so you can get them in the navigationOptions static function.



          You can add the searchFunction as a navigation object param and pass it to the onChangeText attribute.



          The implementation looks like this:



          class MyScreen extends React.Component {

          // Pass the searchFunction from the navigation params to the `onChangeText` attribute.
          // It should be triggered with the `text` argument.
          static navigationOptions = ({ navigation }) =>
          {
          headerLeft: (
          <SearchBar
          placeholder="Search"
          round
          onChangeText={navigation.getParam('searchFunc')}
          />
          )
          };

          // Use arrow function to bind it to the MyScreen class.
          // (I'm not sure you have to do it like this, try to use it as a normal function first)
          searchFunction = (text) => {
          alert( text + ' searched succesfully');
          }

          // Add the `searchFunction` as a navigation param:
          componentDidMount() {
          this.props.navigation.setParams({searchFunc: this.searchFunction})
          }

          // Since we pass a class function as a param
          // I believe it would be a good practice to remove it
          // from the navigation params when the Component unmounts.
          componentWillUnmount() {
          this.props.navigation.setParams({searchFunc: null})
          }

          render() {
          return (<View />);
          }
          }


          Source






          share|improve this answer














          The reserved word this means nothing in the static context of the navigationOptions function, So you can't use it there to call the searchFunction.



          There's a way to add params to the navigation object so you can get them in the navigationOptions static function.



          You can add the searchFunction as a navigation object param and pass it to the onChangeText attribute.



          The implementation looks like this:



          class MyScreen extends React.Component {

          // Pass the searchFunction from the navigation params to the `onChangeText` attribute.
          // It should be triggered with the `text` argument.
          static navigationOptions = ({ navigation }) =>
          {
          headerLeft: (
          <SearchBar
          placeholder="Search"
          round
          onChangeText={navigation.getParam('searchFunc')}
          />
          )
          };

          // Use arrow function to bind it to the MyScreen class.
          // (I'm not sure you have to do it like this, try to use it as a normal function first)
          searchFunction = (text) => {
          alert( text + ' searched succesfully');
          }

          // Add the `searchFunction` as a navigation param:
          componentDidMount() {
          this.props.navigation.setParams({searchFunc: this.searchFunction})
          }

          // Since we pass a class function as a param
          // I believe it would be a good practice to remove it
          // from the navigation params when the Component unmounts.
          componentWillUnmount() {
          this.props.navigation.setParams({searchFunc: null})
          }

          render() {
          return (<View />);
          }
          }


          Source







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered yesterday









          HedeH

          1,8211017




          1,8211017












          • I will give it a try. Thanks for the answer.
            – Umar
            1 hour ago


















          • I will give it a try. Thanks for the answer.
            – Umar
            1 hour ago
















          I will give it a try. Thanks for the answer.
          – Umar
          1 hour ago




          I will give it a try. Thanks for the answer.
          – Umar
          1 hour ago










          Umar is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Umar is a new contributor. Be nice, and check out our Code of Conduct.













          Umar is a new contributor. Be nice, and check out our Code of Conduct.












          Umar is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53346613%2fhow-to-make-a-parameterised-function-call-in-react-native-nav-header%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

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga