從我一直在閱讀的內容來看,錯誤是通過嘗試列印“{menu.props.options[0]}”產生的。我知道我不能像這樣回傳它,因為我必須將它包裝在一個物件或陣列中。
有問題的代碼是:
return (
<Select
labelInValue
filterOption={ false }
showArrow
suffixIcon={ <SearchOutlined /> }
onSearch={ debounceFetcher }
notFoundContent={ fetching ? <Spin size="small" /> : null }
{ ...props }
className="search-repeated-words"
dropdownRender={menu => (
<div>
{menu.props.options[0]}
<div style={{ padding: "0 12px", height: 30, lineHeight: "30px", color: "#46C4C1" }} >
Sugerencias
</div>
<Divider style={{ margin: 5 }} />
{menu}
</div>
)}
>
{
options.map((option, index) => (
<Select.Option key={index} value={option.text}>
<span>{option.text}</span>
</Select.Option>
)
)
}
</Select>
我可以實施什么解決方案?
當我想在螢屏上列印或顯示 {menu.props.options[0]} 時會產生錯誤。我怎么能顯示出來?
uj5u.com熱心網友回復:
我猜{menu.props.options[0]}是一個物件。您應該只顯示此物件的相關屬性,使用原始型別(例如字串),例如menu.props?.options?.[0]?.label
uj5u.com熱心網友回復:
如果錯誤是通過嘗試渲染產生的
{menu.props.options[0]}
那么這意味著 menu.props.option[0] 是一個 Object 型別,你應該像這樣渲染它
<div>{menu.props.options[0].title} {menu.props.options[0].description}</div>
(取決于物件包含哪些屬性)
或者如果您出于某種原因想要按原樣渲染物件:
<div>{ JSON.stringify(menu.props.options[0]) }</div>
uj5u.com熱心網友回復:
創建一個可以呈現option資料的輔助函式(或另一個組件)。下面我創建了一個標簽OptionCell,用于渲染第一個元素以及整個選項陣列:
const OptionCell = (option) => (
<Select.Option key={index} value={option.text}>
<span>{option.text}</span>
</Select.Option>
)
return (
<Select
labelInValue
filterOption={ false }
showArrow
suffixIcon={ <SearchOutlined /> }
onSearch={ debounceFetcher }
notFoundContent={ fetching ? <Spin size="small" /> : null }
{ ...props }
className="search-repeated-words"
dropdownRender={menu => (
<div>
{<OptionCell options={menu.props.options[0]} />)
<div style={{ padding: "0 12px", height: 30, lineHeight: "30px", color: "#46C4C1" }} >
Sugerencias
</div>
<Divider style={{ margin: 5 }} />
{menu}
</div>
)}
>
{
options.map((option, index) => (
<OptionCell options={options} key={index} />
)
}
</Select>
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/487203.html
標籤:javascript 数组 反应 目的 使成为
上一篇:jQuery或Javascript獲取帶有值的選擇選項陣列以隱藏元素
下一篇:對資料關鍵元素運行特定檢查
