1

In react I need to set a prop based on the return value from a function. I want to try to avoid using state for this and I am getting a variable from localstorage so that the user can pick up where they left off. This is the function that returns the value from local storage:

const getCustomerId = () => {
    return getItem('customerId').toString();
};

Then I have a provider that needs access to the customerId from localstorage. I try to pass the functions return value as a prop but the prop is supposed to be a string and its giving it a function still :

<ProfileProvider
      id="1"
      email={user?.email}
      setSelectedCustomer={handleChangeCustomer}
      customerId={getCustomerId}
    >

Also when the handleChangeCustomer is called, the getCustomerId should be called from within it to change. so should the prop of customerId with the updated value from localStorage.

Im not sure how this can done without using state.

handle customer function:

const handleChangeCustomer = id => {
    
    setLocalCustomer(id);
    getCustomerId();
  };

2 Answers 2

1

You need to invoke the function like this:

<ProfileProvider
  id='1'
  email={user?.email}
  setSelectedCustomer={handleChangeCustomer}
  customerId={() => getCustomerId()}
/>;
Sign up to request clarification or add additional context in comments.

4 Comments

can you explain the difference between just adding () and adding an arrow function ()=>{} ?
If you need the function to be referenced so it is called later, then you can use the function expression like this: customerId={etCustomerId} When need the function to be called right NOW then you have to invoke the function like this: customerId={() => getCustomerId()} In your case, you need to get the returned value from the function, so have to call it NOW.
For some reason using () => getCustomerId() didnt work but using getCustomerId() did. Do you know why?
actually there is no much difference between customerId={() => getCustomerId()} and customerId={getCustomerId}. Both situations you are passing a function, to customerId, not invoking the function.
0

//Have you tried adding the parentheses at the end?

<ProfileProvider
      id="1"
      email={user?.email}
      setSelectedCustomer={handleChangeCustomer}
      customerId={getCustomerId()}
    />


//Or adding it to a arrow function:

    <ProfileProvider
          id="1"
          email={user?.email}
          setSelectedCustomer={handleChangeCustomer}
          customerId={()=>{getCustomerId()}}
        />

3 Comments

Yes that works! Is there any way to call getCustomerId() from the handleChangeCustomer function and it updates in the prop?
@ousmane784 Can i see your handleChangeCustomer function?
I added the function. Its just a simple one where it sets the localstorage id and it should update the prop in provider

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.