0

I want to get a specific value in my state array

The state array looks like this if I do console.log

profileModule  
(2) […]
​
0: Object { moduleId: "4E948025-1F1B-41E2-A18D-55AD8646810B", module: "Client Maintenance", description: "Client Enrollment", … }
​
1: Object { moduleId: "C77C1031-2714-483D-AE59-21C9CD5EBAEF", module: "Bank Maintenance", description: "Bank Enrollment", … }
​
length: 2
​
<prototype>: Array []

I want to get the moduleId and set it as String

I tried this, but it doesn't work. It says undefined.

useEffect(() => {
    const moduleId = profileModule.moduleId
    console.log("moduleId ", moduleId)
  }, [profileModule]);

Any help would do. TIA

1 Answer 1

1

Profile Module is an array

You need to get the index of the profile module you would like to retrieve.

useEffect(() => {
    const moduleId = profileModule[0].moduleId // gets the 1st index
    console.log("moduleId ", moduleId)
  }, [profileModule]);

But I don't think you want this to be static. Do you know what index you want to retrieve?

To Retrieve a list of IDs

You can use memo or callback on this code if you want to optimise.

const idsArray = profileModule 
  ? profileModule.map(({moduleId}) => moduleId);
  : [];
Sign up to request clarification or add additional context in comments.

4 Comments

I would like to get all of the moduleId. e.g. "4E948025-1F1B-41E2-A18D-55AD8646810B","C77C1031-2714-483D-AE59-21C9CD5EBAEF"
Dont use useEffect. You can use useCallback to asign a value outside of the callback.
And after trying your code, it says profileModule[0].moduleId is undefined
Would this be correct, if I'm trying to get moduleId by row index? const getModuleIds = useCallback((rowIndex) => { const moduleId = profileModule[rowIndex].moduleId console.log("moduleId ", moduleId) })

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.