1

In react native, I am doing a console log like:

console.log(props);

I am getting full object navigation of stack navigation.

But when I do this:

console.log(JSON.stringify(props));

I am getting the navigation object undefined or empty.

Screenshots attached

enter image description here

enter image description here

enter image description here

enter image description here

BUY
  • 705
  • 1
  • 11
  • 30
talha_ah
  • 314
  • 6
  • 12
  • 2
    The values of all properties in `navigation` are functions, and JSON does only supports object, arrays and primitives as values. – t.niese Jul 09 '20 at 11:19

2 Answers2

0

I'll tell you the exact reason why it doesnt work,

when you console.log it just prints out whatever it is, if its a function, it describes as [Function] .

But JSON.stringify doesnt stringify a function , hence when in this.props, navigation is an object, but inside it, it stores function , so key is a string but value is a function.

check this :

"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]},

You can see key is addListener , but value is [Function addListener] , so hence its a function, hence the whole navigation object becomes an empty object.

Hope its clear. feel free for doubts

Gaurav Roy
  • 11,175
  • 3
  • 24
  • 45
0

From MDN

Link: JSON.stringify

JSON.stringify() converts a value to JSON notation representing it:

  • If the value has a toJSON() method, it's responsible to define what data will be serialized.
  • Boolean, Number, and String objects are converted to the corresponding primitive values during stringification, in accord with the traditional conversion semantics.
  • undefined, Functions, and Symbols are not valid JSON values. If any such values are encountered during conversion they are either omitted (when found in an object) or changed to null (when found in an array). JSON.stringify() can return undefined when passing in "pure" values like JSON.stringify(function(){}) or JSON.stringify(undefined).
  • All Symbol-keyed properties will be completely ignored, even when using the replacer function.
  • The instances of Date implement the toJSON() function by returning a string (the same as date.toISOString()). Thus, they are treated as strings.
  • The numbers Infinity and NaN, as well as the value null, are all considered null.
  • All the other Object instances (including Map, Set, WeakMap, and WeakSet) will have only their enumerable properties serialized.