How can I get my returning value from a function in GlobalContext?

helloWorld

I have two functions, one is GetUsers(), other one is GetDiscussion() in GlobalContextManager.js

In MainScreen.js I can get returning value from GetUsers(), but i cant get value from GetDiscussion()

Here is GlobalContextManager.js

    import React, { createContext, useContext, useState } from 'react';
    import HorizontalCircles from "../components/HorizontalDiscussion";
    import HorizontalDiscussion from "../components/HorizontalDiscussion";
    
    
    export const GlobalContext = createContext();
    
    function GlobalContextManager(props) {
    
      const GetUsers = () => {
        const returnFromService = {
          "errorCode": -1,
          "data": {
            "colors": [
              {
                colorFirst:"red",
                colorSecond:"black",
              },
              {
                colorFirst:"pink",
                colorSecond:"gray",
              }
            ]
          }
        }; 
    
        if (returnFromService.errorCode === -1) {
          const returnFromGlobal = returnFromService.data.colors;
          return returnFromGlobal;
        } else {
          return returnFromService.errorCode;
        }
      }
    
      const GetDiscussion = () => {
        const returnFromService = {
          "errorCode": -1,
          "data": {
            "cards": [
              {
                "isLive": true,
                "type": "Topic",
                "title": "Human Resources Analysis",
                "author": "Emran Emon",
                "attendees": 12,
                "color": "#FFF9F2",
                "circles": [
                  "white",
                  "yellow",
                  "red",
                  "gray"
                ],
                "attendeesColor": "pink"
              },
              {
                "isLive": false,
                "type": "Topic",
                "title": "React Native Course UI",
                "author": "Matias Delgado",
                "attendees": 12,
                "color": "#E7FBFF",
                "circles": [
                  "white",
                  "yellow",
                  "red",
                  "gray"
                ],
                "attendeesColor": "pink"
              }
            ]
          }
        }
      }
    
      if (returnFromService.errorCode === -1) {


const returnFromGlobal = returnFromService.data.cards;

        return returnFromGlobal;
      } else {
        return returnFromService.errorCode;
      }
    
     
    
      return (
        <GlobalContext.Provider value={{ GetUsers, GetDiscussion }}>
          {props.children}
        </GlobalContext.Provider>
      );
    }
    
    export default GlobalContextManager;

Here is related part in MainScreen.js

import { GlobalContext } from '../../context/GlobalContextManager';
const MainScreen = ({ navigation }) => {
  const global = useContext(GlobalContext);

  const getUsers = () => {

    console.log("users from global:",global.GetUsers());

    const g_users =  global.GetUsers();
    const tmpUsers = g_users.map((a,index) =>  <HorizontalCircles key={index} colorFirst={a.colorFirst} colorSecond={a.colorSecond} />)
    
    setTimeout(() => {
      setUsers(tmpUsers);
    }, 5000);

  }

const getDiscussion = () => {
    console.log("Getting Discussion Card")
    const g_discussion = global.GetDiscussion();
    const tmpHorizontal = g_discussion.map((a, index) => <HorizontalDiscussion key={index} color={a.color} /> )

    setTimeout(() => {
      setHorizontalDiscussion(tmpHorizontal);
    }, 3000);

  }

The first function works, but the other doesnt. It says "can't find variable:returnFromService" for the second one. I thought it would work if i changed this variable name because i used it in first function, but it still didnt work.

Sabir

try to replace your code with following Actually you were using variable after enclosing function's braces

import React, { createContext, useContext, useState } from 'react';
import HorizontalCircles from "../components/HorizontalDiscussion";
import HorizontalDiscussion from "../components/HorizontalDiscussion";


export const GlobalContext = createContext();

function GlobalContextManager(props) {

  const GetUsers = () => {
    const returnFromService = {
      "errorCode": -1,
      "data": {
        "colors": [
          {
            colorFirst:"red",
            colorSecond:"black",
          },
          {
            colorFirst:"pink",
            colorSecond:"gray",
          }
        ]
      }
    }; 

    if (returnFromService.errorCode === -1) {
      const returnFromGlobal = returnFromService.data.colors;
      return returnFromGlobal;
    } else {
      return returnFromService.errorCode;
    }
  }

  const GetDiscussion = () => {
    const returnFromService = {
      "errorCode": -1,
      "data": {
        "cards": [
          {
            "isLive": true,
            "type": "Topic",
            "title": "Human Resources Analysis",
            "author": "Emran Emon",
            "attendees": 12,
            "color": "#FFF9F2",
            "circles": [
              "white",
              "yellow",
              "red",
              "gray"
            ],
            "attendeesColor": "pink"
          },
          {
            "isLive": false,
            "type": "Topic",
            "title": "React Native Course UI",
            "author": "Matias Delgado",
            "attendees": 12,
            "color": "#E7FBFF",
            "circles": [
              "white",
              "yellow",
              "red",
              "gray"
            ],
            "attendeesColor": "pink"
          }
        ]
      }
    }
  // } remove it from here

      if (returnFromService.errorCode === -1) {
        const returnFromGlobal = returnFromService.data.colors;
        return returnFromGlobal;
      } else {
        return returnFromService.errorCode;
      }
  } // put it here

  return (
    <GlobalContext.Provider value={{ GetUsers, GetDiscussion }}>
      {props.children}
    </GlobalContext.Provider>
  );
}

export default GlobalContextManager;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I reach the variable which i named "global" from GlobalContext?

How do I get the returning value of a function that can throw that I'm testing with XCTAssertNoThrow(...)

How can i get value from an Rxjs observable in my code?

How can I get the selected value from my <select>

How can I get the exact value from the IPL function?

How I can get second value from function using *pointer?

How can I get the parameters of a function from a function passed as a parameter to my function, in Lua?

How can I access the result I get from my GET request inside res.on function?

Why can't my local PHP function see the $test variable, and how can I get its value?

How can I refrain from using import and still get the same output from my function?

How can I get the number of elements in a an array of structs if returned from a function? +How to handle memory leaks when returning?

How I can call javascript function and get the return value from javascript function

How can i render my image value as array from the local storage in function component?

how can i pass value from javascript to my php function codeigniter

how I can get the value from datalist

How can I get this value from a JFileChooser?

How can I get a value from LiveData?

how can I get the value from json?

How can I get value from GroupedObservable?

How can I get value from option?

How can i get the value from json

How can I get a value from an URL?

How can i get value from textbox?

How can I get a value from an array?

How can I pass data from angular to my cloud function and get a response back?

How can I get the real image value from each item in my list and subscribe it to another list?

How can I get a specific value from my JSON object without using Array.filter()?

How can I get values from my SQL query if the column has zero value?

how can i get value from input text and show to my modal?