如何在React Hooks SPFx Web部件中清除MGT PeoplePicker?

huangapple go评论61阅读模式
英文:

How to clear the MGT PeoplePicker in react hooks SPFx web part?

问题

需要在使用后清除 PeoplePicker

<PeoplePicker
  selectedPeople={addedMembers}
  selectionMode={"multiple"}
  show-max={50}
  selectionChanged={onPeoplePickerChanged}
/>;

所以我将 addedMembers 设置为 [],但它不起作用。

我看到一个涉及到 ref 的答案 这里,但它使用了类,而我只知道 hooks。
有人可以帮忙吗?

谢谢

英文:

I need to clear the the PeoplePicker after use.

<PeoplePicker
  selectedPeople={addedMembers}
  selectionMode={"multiple"}
  show-max={50}
  selectionChanged={onPeoplePickerChanged}
/>;

So I set addedMembers to [] but it does not work.

I saw an answer involving ref here but it uses a class and I only know hooks.
Anyone can help?

Thank you

答案1

得分: 1

看例子 https://stackblitz.com/edit/react-ts-zayywf?file=App.tsx

创建一个引用 const ppl = React.useRef(null);

添加到 <PeoplePicker ref={ppl}

然后当你想要清除时,将 selectedPeople 设置为 [] 并运行 ppl.current.focus();

希望能帮助其他人

英文:

See example https://stackblitz.com/edit/react-ts-zayywf?file=App.tsx

make a ref const ppl = React.useRef(null);

add to the <PeoplePicker ref={ppl}

then when you want to clear set the selectedPeople to [] and run ppl.current.focus();

Hope it helps others

huangapple
  • 本文由 发表于 2023年3月12日 16:49:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定