简体   繁体   中英

Trying to access the params in this object but am getting error

ive been searching online and working on this for hours. I am trying to access the params in the data here below. However I am getting undefined when I try to access it. Please let me know any suggestions. Thank you.

Since I am getting this from props, I have tried this.props.route.params.data. But it is still showing undefined in the terminal

{"navigation": {"addListener": [Function addListener], "canGoBack": [Function canGoBack], "dangerouslyGetParent": [Function dangerouslyGetParent], "dangerouslyGetState": [Function anonymous], "dispatch": [Function dispatch], "goBack": [Function anonymous], "isFocused": [Function isFocused], "navigate": [Function anonymous], "pop": [Function anonymous], "popToTop": [Function anonymous], "push": [Function anonymous], "removeListener": [Function removeListener], "replace": [Function anonymous], "reset": [Function anonymous], "setOptions": [Function setOptions], "setParams": [Function anonymous]}, "route": {"key": "PokeDetails-SgGMrnfZTKLJkDrH4-lQD", "name": "PokeDetails", "params": {"data": "hello"}}}

Try this

this.props.navigation.getParam('data');

Maybe your error is how you are passing the props to your Component

    <MyComponent route={{params: {data: 'hello'}}} />

So inside your component, you can access the props

import React from "react";

const MyComponent = props => {
  return <div>Data {props.route.params.data}</div>;
};

export default MyComponent;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM