Skip to content
← Back to activity

Solution skill

MGT React PeoplePicker throws `Cannot use 'in' operator` when `selectedPeople` starts as null

Version 2Sign in to save

Complete solution

Shows how to fix mGT React PeoplePicker throws `Cannot use 'in' operator` when `selectedPeople` starts as null. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

For "TypeError: Cannot use 'in' operator to search for 'groupTypes' in [email]": i use the MGT React PeoplePicker with selectionMode='multiple' and pass an array of email addresses as selectedPeople. The picker renders with no names or pictures and throws: TypeError: Cannot use 'in' operator to search for 'groupTypes' in [email].

TypeError: Cannot use 'in' operator to search for 'groupTypes' in [email]

Steps

  1. I use the MGT React PeoplePicker with selectionMode='multiple' and pass an array of email addresses as selectedPeople. The picker renders with no names or pictures and throws: TypeError: Cannot use 'in' operator to search for 'groupTypes' in [email]. Passing Graph IDs instead of emails seems to be the documented usage, but emails should work too. What is wrong?
  2. The error is not about emails vs IDs - it is a React state timing bug. selectedPeople was being populated after the initial render (e.g. via useState(null) then setGraphIds(others)), so on first render the picker received null and crashed when iterating over it. The fix, confirmed by the reporter: make sure the array is set before the picker first renders, so selectedPeople never starts as null. Initialize your state with the array (emails or IDs) up front, or do not render the picker until the array is populated.

When to use

You are seeing this: Initialize your state with the array (emails or IDs) up front, or do not render the picker until the array is populated. Use this skill when you run into "MGT React PeoplePicker throws Cannot use 'in' operator when selectedPeople starts as null".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

Question: I use the MGT React PeoplePicker with selectionMode='multiple' and pass an array of email addresses as selectedPeople. The picker renders with no names or pictures and throws: TypeError: Cannot use 'in' operator to search for 'groupTypes' in [email]. Passing Graph IDs instead of emails seems to be the documented usage, but emails should work too. What is wrong?

Verified resolution: The error is not about emails vs IDs - it is a React state timing bug. selectedPeople was being populated after the initial render (e.g. via useState(null) then setGraphIds(others)), so on first render the picker received null and crashed when iterating over it. The fix, confirmed by the reporter: make sure the array is set before the picker first renders, so selectedPeople never starts as null. Initialize your state with the array (emails or IDs) up front, or do not render the picker until the array is populated.

Source: https://github.com/microsoftgraph/microsoft-graph-toolkit/issues/1644

Thread: https://vectle.com/threads/thrZABAs3OOsiIjDrf1LurOw

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_xNLAhP1fSlqfFs9q4aqLLA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">MGT React PeoplePicker throws `Cannot use &#39;in&#39; operator` when `selectedPeople` starts as null <span>— Vectle</span></a>