Javascript 材料-带有特殊原因的方式<;选择/>;不应关闭时关闭的菜单';T 当前行为

Javascript 材料-带有特殊原因的方式<;选择/>;不应关闭时关闭的菜单';T 当前行为,javascript,reactjs,material-ui,Javascript,Reactjs,Material Ui,将组件与multiple一起使用时,用户应能够选择多个选项,而无需关闭菜单。当实例化组件时,这与预期一样有效,但当将其与with styles()HOC一起使用时,它会自动关闭 演示: //这里我们使用了HOC,这就是它中断的地方。 const NewSelect=(道具)=>{ const StyledSelect=with styles({ //风格等 },{name:“NewSelect”}(选择); 返回; }; //选择项目时关闭 //不关闭(所需的行为) 预期行为您应该将Styl

组件与
multiple
一起使用时,用户应能够选择多个选项,而无需关闭菜单。当实例化
组件时,这与预期一样有效,但当将其与
with styles()
HOC一起使用时,它会自动关闭

演示:

//这里我们使用了HOC,这就是它中断的地方。
const NewSelect=(道具)=>{
const StyledSelect=with styles({
//风格等
},{name:“NewSelect”}(选择);
返回;
};
//选择项目时关闭
//不关闭(所需的行为)

预期行为您应该将
StyledSelect
声明移到
Select2
之外,因为每次输入更改都会导致
Select2
重新提交(我在其中放置了一个
控制台。日志
,以便您清楚地看到),StyledSelect会再次声明,这会导致您的未指定行为

解决方案

const StyledSelect = withStyles({})(Select);

const Select2 = (props) => {
  console.log("rerender");
  // If we chance StyledSelect to Select (Mui one), it works fine.
  return <StyledSelect {...props}>{props.children}</StyledSelect>;
};
constyledselect=withStyles({})(Select);
const Select2=(道具)=>{
控制台日志(“重新加载”);
//如果我们偶然选择(Mui one),它可以正常工作。
返回{props.children};
};
演示


您应该将
StyledSelect
声明移到
Select2
之外,因为每次输入更改都会导致
Select2
重新提交(我在其中放置了一个
控制台。日志
,以便您清楚地看到),StyledSelect会再次声明,这会导致您的行为异常

解决方案

const StyledSelect = withStyles({})(Select);

const Select2 = (props) => {
  console.log("rerender");
  // If we chance StyledSelect to Select (Mui one), it works fine.
  return <StyledSelect {...props}>{props.children}</StyledSelect>;
};
constyledselect=withStyles({})(Select);
const Select2=(道具)=>{
控制台日志(“重新加载”);
//如果我们偶然选择(Mui one),它可以正常工作。
返回{props.children};
};
演示


谢谢!我还需要将道具传递到withStyles中第一个参数的对象中。已尝试将其转换为箭头函数,但我得到“对象作为子对象无效”。:(不管我怎么想:@LucasArundell很酷,我以前从未注意到这种方式,TILThanks!不过我还需要在withStyles中向第一个参数的对象传递道具。我尝试将其转换为箭头函数,但得到的结果是“对象作为反应子无效”。:(不管我怎么想:@LucasArundell酷,我以前从未注意到这种情况,直到