2017-04-08 71 views
1

this answer中解釋瞭如何使用未記錄的is功能將自定義屬性添加到R​​eact元素。但是,我想添加一個用於括號括起來的amp-bind的屬性。當我嘗試此屬性不添加:帶括號的自定義反應屬性

const ampProps = { 
    '[class]': 'foo.bar', 
}; 

return <div is {...ampProps}><MyComponent></div> 

如何添加自定義屬性?

+0

常量ampProps =類=>({ [類] :'foo.bar', }); return

elmeister

+0

重點是在支架中有括號。你提出的解決方案不會那樣做 –

+0

這有幫助嗎? https://github.com/zeit/next.js/blob/master/examples/with-amp/pages/_document.js –

回答

0

您可能需要設置React才能使用ReactInjection接受許多AMP指令。

完成之後,React生成的HTML將包含它。

import { DOMProperty } from 'react-dom/lib/ReactInjection' 
DOMProperty.injectDOMPropertyConfig({ 
    Properties: { 
     '[class]': DOMProperty.MUST_USE_PROPERTY 
    } 
}) 

UPDATE(AUG-25​​-2017):

上面的代碼解決了像DIV,UL,跨度正規的HTML標籤的情況下,等

我目前存在[src]屬性在自定義放大器列表中的問題。 React代碼不會爲自定義標籤注入DOM屬性,代碼認爲以'['開頭的屬性不是「安全的」,並將其從「amp-list」中移除,而不管injectDOMPropertyConfig的設置如何。

沒有爲陣營16 RFC將解決此問題:https://github.com/facebook/react/issues/10399

我也放置在一個問題: https://github.com/facebook/react/pull/10385