Passing Values to new Screen in React native












1















I have a View that is clickable using touchable opacity which has an item.address and item.price that renders inside.



<TouchableOpacity onPress = {this.onShowHomeDetails.bind()}>
<View style = {{height: 130,width :130,
marginLeft : 20, borderWidth: 0.5, borderColor: '#dddddd'}} >
<View style={{flex: 2}}>
<Image source={{uri : item.imageUrl}}
style={{flex:1, width: null,
height: null, resizeMode: 'cover'}}/>
</View>
<View style={{flex: 1 , paddingLeft: 10,paddingTop: 10}}>
<Text style={{fontWeight:"bold", fontSize :12}}>
{item.address}
</Text>
{this.getIndividualHouseInfo(item.address)}
</View>
<View style={{flex: 1 , paddingLeft: 10}}>
<Text> ${item.price} </Text>
</View>
</View>
</TouchableOpacity>


How do I pass these values through navigation to another screen using the onShowHomeDetails function?



onShowHomeDetails = () => {
this.props.navigation.navigate('HomeDetails');
}


And my home detail component which is the destination screen is



class HomeDetails extends Component{
constructor(props){
super(props);
this.state = {
address : this.props.state.params.address,
};
}
render(){
return(
<View style ={styles.container}>
<Text>Details </Text>
<Text>{this.state.address}</Text>
</View>
);
}
}









share|improve this question





























    1















    I have a View that is clickable using touchable opacity which has an item.address and item.price that renders inside.



    <TouchableOpacity onPress = {this.onShowHomeDetails.bind()}>
    <View style = {{height: 130,width :130,
    marginLeft : 20, borderWidth: 0.5, borderColor: '#dddddd'}} >
    <View style={{flex: 2}}>
    <Image source={{uri : item.imageUrl}}
    style={{flex:1, width: null,
    height: null, resizeMode: 'cover'}}/>
    </View>
    <View style={{flex: 1 , paddingLeft: 10,paddingTop: 10}}>
    <Text style={{fontWeight:"bold", fontSize :12}}>
    {item.address}
    </Text>
    {this.getIndividualHouseInfo(item.address)}
    </View>
    <View style={{flex: 1 , paddingLeft: 10}}>
    <Text> ${item.price} </Text>
    </View>
    </View>
    </TouchableOpacity>


    How do I pass these values through navigation to another screen using the onShowHomeDetails function?



    onShowHomeDetails = () => {
    this.props.navigation.navigate('HomeDetails');
    }


    And my home detail component which is the destination screen is



    class HomeDetails extends Component{
    constructor(props){
    super(props);
    this.state = {
    address : this.props.state.params.address,
    };
    }
    render(){
    return(
    <View style ={styles.container}>
    <Text>Details </Text>
    <Text>{this.state.address}</Text>
    </View>
    );
    }
    }









    share|improve this question



























      1












      1








      1








      I have a View that is clickable using touchable opacity which has an item.address and item.price that renders inside.



      <TouchableOpacity onPress = {this.onShowHomeDetails.bind()}>
      <View style = {{height: 130,width :130,
      marginLeft : 20, borderWidth: 0.5, borderColor: '#dddddd'}} >
      <View style={{flex: 2}}>
      <Image source={{uri : item.imageUrl}}
      style={{flex:1, width: null,
      height: null, resizeMode: 'cover'}}/>
      </View>
      <View style={{flex: 1 , paddingLeft: 10,paddingTop: 10}}>
      <Text style={{fontWeight:"bold", fontSize :12}}>
      {item.address}
      </Text>
      {this.getIndividualHouseInfo(item.address)}
      </View>
      <View style={{flex: 1 , paddingLeft: 10}}>
      <Text> ${item.price} </Text>
      </View>
      </View>
      </TouchableOpacity>


      How do I pass these values through navigation to another screen using the onShowHomeDetails function?



      onShowHomeDetails = () => {
      this.props.navigation.navigate('HomeDetails');
      }


      And my home detail component which is the destination screen is



      class HomeDetails extends Component{
      constructor(props){
      super(props);
      this.state = {
      address : this.props.state.params.address,
      };
      }
      render(){
      return(
      <View style ={styles.container}>
      <Text>Details </Text>
      <Text>{this.state.address}</Text>
      </View>
      );
      }
      }









      share|improve this question
















      I have a View that is clickable using touchable opacity which has an item.address and item.price that renders inside.



      <TouchableOpacity onPress = {this.onShowHomeDetails.bind()}>
      <View style = {{height: 130,width :130,
      marginLeft : 20, borderWidth: 0.5, borderColor: '#dddddd'}} >
      <View style={{flex: 2}}>
      <Image source={{uri : item.imageUrl}}
      style={{flex:1, width: null,
      height: null, resizeMode: 'cover'}}/>
      </View>
      <View style={{flex: 1 , paddingLeft: 10,paddingTop: 10}}>
      <Text style={{fontWeight:"bold", fontSize :12}}>
      {item.address}
      </Text>
      {this.getIndividualHouseInfo(item.address)}
      </View>
      <View style={{flex: 1 , paddingLeft: 10}}>
      <Text> ${item.price} </Text>
      </View>
      </View>
      </TouchableOpacity>


      How do I pass these values through navigation to another screen using the onShowHomeDetails function?



      onShowHomeDetails = () => {
      this.props.navigation.navigate('HomeDetails');
      }


      And my home detail component which is the destination screen is



      class HomeDetails extends Component{
      constructor(props){
      super(props);
      this.state = {
      address : this.props.state.params.address,
      };
      }
      render(){
      return(
      <View style ={styles.container}>
      <Text>Details </Text>
      <Text>{this.state.address}</Text>
      </View>
      );
      }
      }






      reactjs react-native






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 9:40









      varit05

      1,640716




      1,640716










      asked Nov 22 '18 at 3:46









      ButtersnipsButtersnips

      244




      244
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Cool I feel its easy.



          Call function:



          <TouchableOpacity onPress={this.onShowHomeDetails.bind(this, item)}>
          <View style = {{height: 130,width :130,
          marginLeft : 20, borderWidth: 0.5,
          borderColor: '#dddddd'}} >
          <View style={{flex: 2}}>
          <Image source={{uri : item.imageUrl}}
          style={{flex:1, width: null,
          height: null, resizeMode: 'cover'}}
          />
          </View>
          <View style={{flex: 1 , paddingLeft: 10,paddingTop: 10}}>
          <Text style={{fontWeight:"bold" , fontSize :12}}> {item.address} </Text>
          {this.getIndividualHouseInfo(item.address)}
          </View>
          <View style={{flex: 1 , paddingLeft: 10}}>
          <Text> ${item.price} </Text>
          </View>
          </View>
          </TouchableOpacity>


          Writing function:



           onShowHomeDetails = (item) => {
          this.props.navigation.navigate('HomeDetails', {item: item});
          }


          Get params in appropriate screen:



          class HomeDetails extends Component{
          constructor(props){
          super(props);
          this.state = {
          address : props.navigation.getParam('item', '');
          };
          }
          render(){
          return(
          <View style ={styles.container}>
          <Text>Details </Text>
          <Text>{this.state.address}</Text>
          </View>
          );
          }
          }


          Few important things we should know about passsing value as navigation params.




          1. Passing params and getParams both name should be same.


          2. In getparams method we set empty string(''). Because if value is not

            passed, its return empty string



          Refer this document it will help you. https://reactnavigation.org/docs/en/params.html






          share|improve this answer
























          • Very close . the function should be onShowHomeDetails = (item) => { this.props.navigation.navigate('HomeDetails' , {address : item.address}); }. Thanks a ton.

            – Buttersnips
            Nov 22 '18 at 7:01





















          0














          You can pass data while navigating to the state.



          onShowHomeDetails = () => {
          this.props.navigation.navigate('HomeDetails', { home: 'abc'});
          // you can pass any type of object in here as well
          }


          As per the react Navigation doc. The second parameter is the params to pass to destination route.




          navigation.navigate({ routeName, params, action, key })




          and use it in another component with props.
          Option 1:



          this.props.navigation.getParam('home')


          And if at all home is undefined, option 1 fails but with option 2 it will fallback to bydefaultHome



          Option 2:



          this.props.navigation.getParam('home', 'bydefaultHome');


          Hope this helps!






          share|improve this answer

























            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%2f53423590%2fpassing-values-to-new-screen-in-react-native%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Cool I feel its easy.



            Call function:



            <TouchableOpacity onPress={this.onShowHomeDetails.bind(this, item)}>
            <View style = {{height: 130,width :130,
            marginLeft : 20, borderWidth: 0.5,
            borderColor: '#dddddd'}} >
            <View style={{flex: 2}}>
            <Image source={{uri : item.imageUrl}}
            style={{flex:1, width: null,
            height: null, resizeMode: 'cover'}}
            />
            </View>
            <View style={{flex: 1 , paddingLeft: 10,paddingTop: 10}}>
            <Text style={{fontWeight:"bold" , fontSize :12}}> {item.address} </Text>
            {this.getIndividualHouseInfo(item.address)}
            </View>
            <View style={{flex: 1 , paddingLeft: 10}}>
            <Text> ${item.price} </Text>
            </View>
            </View>
            </TouchableOpacity>


            Writing function:



             onShowHomeDetails = (item) => {
            this.props.navigation.navigate('HomeDetails', {item: item});
            }


            Get params in appropriate screen:



            class HomeDetails extends Component{
            constructor(props){
            super(props);
            this.state = {
            address : props.navigation.getParam('item', '');
            };
            }
            render(){
            return(
            <View style ={styles.container}>
            <Text>Details </Text>
            <Text>{this.state.address}</Text>
            </View>
            );
            }
            }


            Few important things we should know about passsing value as navigation params.




            1. Passing params and getParams both name should be same.


            2. In getparams method we set empty string(''). Because if value is not

              passed, its return empty string



            Refer this document it will help you. https://reactnavigation.org/docs/en/params.html






            share|improve this answer
























            • Very close . the function should be onShowHomeDetails = (item) => { this.props.navigation.navigate('HomeDetails' , {address : item.address}); }. Thanks a ton.

              – Buttersnips
              Nov 22 '18 at 7:01


















            1














            Cool I feel its easy.



            Call function:



            <TouchableOpacity onPress={this.onShowHomeDetails.bind(this, item)}>
            <View style = {{height: 130,width :130,
            marginLeft : 20, borderWidth: 0.5,
            borderColor: '#dddddd'}} >
            <View style={{flex: 2}}>
            <Image source={{uri : item.imageUrl}}
            style={{flex:1, width: null,
            height: null, resizeMode: 'cover'}}
            />
            </View>
            <View style={{flex: 1 , paddingLeft: 10,paddingTop: 10}}>
            <Text style={{fontWeight:"bold" , fontSize :12}}> {item.address} </Text>
            {this.getIndividualHouseInfo(item.address)}
            </View>
            <View style={{flex: 1 , paddingLeft: 10}}>
            <Text> ${item.price} </Text>
            </View>
            </View>
            </TouchableOpacity>


            Writing function:



             onShowHomeDetails = (item) => {
            this.props.navigation.navigate('HomeDetails', {item: item});
            }


            Get params in appropriate screen:



            class HomeDetails extends Component{
            constructor(props){
            super(props);
            this.state = {
            address : props.navigation.getParam('item', '');
            };
            }
            render(){
            return(
            <View style ={styles.container}>
            <Text>Details </Text>
            <Text>{this.state.address}</Text>
            </View>
            );
            }
            }


            Few important things we should know about passsing value as navigation params.




            1. Passing params and getParams both name should be same.


            2. In getparams method we set empty string(''). Because if value is not

              passed, its return empty string



            Refer this document it will help you. https://reactnavigation.org/docs/en/params.html






            share|improve this answer
























            • Very close . the function should be onShowHomeDetails = (item) => { this.props.navigation.navigate('HomeDetails' , {address : item.address}); }. Thanks a ton.

              – Buttersnips
              Nov 22 '18 at 7:01
















            1












            1








            1







            Cool I feel its easy.



            Call function:



            <TouchableOpacity onPress={this.onShowHomeDetails.bind(this, item)}>
            <View style = {{height: 130,width :130,
            marginLeft : 20, borderWidth: 0.5,
            borderColor: '#dddddd'}} >
            <View style={{flex: 2}}>
            <Image source={{uri : item.imageUrl}}
            style={{flex:1, width: null,
            height: null, resizeMode: 'cover'}}
            />
            </View>
            <View style={{flex: 1 , paddingLeft: 10,paddingTop: 10}}>
            <Text style={{fontWeight:"bold" , fontSize :12}}> {item.address} </Text>
            {this.getIndividualHouseInfo(item.address)}
            </View>
            <View style={{flex: 1 , paddingLeft: 10}}>
            <Text> ${item.price} </Text>
            </View>
            </View>
            </TouchableOpacity>


            Writing function:



             onShowHomeDetails = (item) => {
            this.props.navigation.navigate('HomeDetails', {item: item});
            }


            Get params in appropriate screen:



            class HomeDetails extends Component{
            constructor(props){
            super(props);
            this.state = {
            address : props.navigation.getParam('item', '');
            };
            }
            render(){
            return(
            <View style ={styles.container}>
            <Text>Details </Text>
            <Text>{this.state.address}</Text>
            </View>
            );
            }
            }


            Few important things we should know about passsing value as navigation params.




            1. Passing params and getParams both name should be same.


            2. In getparams method we set empty string(''). Because if value is not

              passed, its return empty string



            Refer this document it will help you. https://reactnavigation.org/docs/en/params.html






            share|improve this answer













            Cool I feel its easy.



            Call function:



            <TouchableOpacity onPress={this.onShowHomeDetails.bind(this, item)}>
            <View style = {{height: 130,width :130,
            marginLeft : 20, borderWidth: 0.5,
            borderColor: '#dddddd'}} >
            <View style={{flex: 2}}>
            <Image source={{uri : item.imageUrl}}
            style={{flex:1, width: null,
            height: null, resizeMode: 'cover'}}
            />
            </View>
            <View style={{flex: 1 , paddingLeft: 10,paddingTop: 10}}>
            <Text style={{fontWeight:"bold" , fontSize :12}}> {item.address} </Text>
            {this.getIndividualHouseInfo(item.address)}
            </View>
            <View style={{flex: 1 , paddingLeft: 10}}>
            <Text> ${item.price} </Text>
            </View>
            </View>
            </TouchableOpacity>


            Writing function:



             onShowHomeDetails = (item) => {
            this.props.navigation.navigate('HomeDetails', {item: item});
            }


            Get params in appropriate screen:



            class HomeDetails extends Component{
            constructor(props){
            super(props);
            this.state = {
            address : props.navigation.getParam('item', '');
            };
            }
            render(){
            return(
            <View style ={styles.container}>
            <Text>Details </Text>
            <Text>{this.state.address}</Text>
            </View>
            );
            }
            }


            Few important things we should know about passsing value as navigation params.




            1. Passing params and getParams both name should be same.


            2. In getparams method we set empty string(''). Because if value is not

              passed, its return empty string



            Refer this document it will help you. https://reactnavigation.org/docs/en/params.html







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 5:08









            EDISON JEDISON J

            24516




            24516













            • Very close . the function should be onShowHomeDetails = (item) => { this.props.navigation.navigate('HomeDetails' , {address : item.address}); }. Thanks a ton.

              – Buttersnips
              Nov 22 '18 at 7:01





















            • Very close . the function should be onShowHomeDetails = (item) => { this.props.navigation.navigate('HomeDetails' , {address : item.address}); }. Thanks a ton.

              – Buttersnips
              Nov 22 '18 at 7:01



















            Very close . the function should be onShowHomeDetails = (item) => { this.props.navigation.navigate('HomeDetails' , {address : item.address}); }. Thanks a ton.

            – Buttersnips
            Nov 22 '18 at 7:01







            Very close . the function should be onShowHomeDetails = (item) => { this.props.navigation.navigate('HomeDetails' , {address : item.address}); }. Thanks a ton.

            – Buttersnips
            Nov 22 '18 at 7:01















            0














            You can pass data while navigating to the state.



            onShowHomeDetails = () => {
            this.props.navigation.navigate('HomeDetails', { home: 'abc'});
            // you can pass any type of object in here as well
            }


            As per the react Navigation doc. The second parameter is the params to pass to destination route.




            navigation.navigate({ routeName, params, action, key })




            and use it in another component with props.
            Option 1:



            this.props.navigation.getParam('home')


            And if at all home is undefined, option 1 fails but with option 2 it will fallback to bydefaultHome



            Option 2:



            this.props.navigation.getParam('home', 'bydefaultHome');


            Hope this helps!






            share|improve this answer






























              0














              You can pass data while navigating to the state.



              onShowHomeDetails = () => {
              this.props.navigation.navigate('HomeDetails', { home: 'abc'});
              // you can pass any type of object in here as well
              }


              As per the react Navigation doc. The second parameter is the params to pass to destination route.




              navigation.navigate({ routeName, params, action, key })




              and use it in another component with props.
              Option 1:



              this.props.navigation.getParam('home')


              And if at all home is undefined, option 1 fails but with option 2 it will fallback to bydefaultHome



              Option 2:



              this.props.navigation.getParam('home', 'bydefaultHome');


              Hope this helps!






              share|improve this answer




























                0












                0








                0







                You can pass data while navigating to the state.



                onShowHomeDetails = () => {
                this.props.navigation.navigate('HomeDetails', { home: 'abc'});
                // you can pass any type of object in here as well
                }


                As per the react Navigation doc. The second parameter is the params to pass to destination route.




                navigation.navigate({ routeName, params, action, key })




                and use it in another component with props.
                Option 1:



                this.props.navigation.getParam('home')


                And if at all home is undefined, option 1 fails but with option 2 it will fallback to bydefaultHome



                Option 2:



                this.props.navigation.getParam('home', 'bydefaultHome');


                Hope this helps!






                share|improve this answer















                You can pass data while navigating to the state.



                onShowHomeDetails = () => {
                this.props.navigation.navigate('HomeDetails', { home: 'abc'});
                // you can pass any type of object in here as well
                }


                As per the react Navigation doc. The second parameter is the params to pass to destination route.




                navigation.navigate({ routeName, params, action, key })




                and use it in another component with props.
                Option 1:



                this.props.navigation.getParam('home')


                And if at all home is undefined, option 1 fails but with option 2 it will fallback to bydefaultHome



                Option 2:



                this.props.navigation.getParam('home', 'bydefaultHome');


                Hope this helps!







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 22 '18 at 4:16

























                answered Nov 22 '18 at 4:11









                varit05varit05

                1,640716




                1,640716






























                    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%2f53423590%2fpassing-values-to-new-screen-in-react-native%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

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    RAC Tourist Trophy