2016-04-07 38 views
0

使用給出的React-Bootstrap和react.rb的例子完美的工作,但我試圖得到一個名爲React-TimeAgo的NPN組件工作,我迷路了。如何將基於JavaScript或JSX的組件導入React.rb,以便在Ruby中可訪問?

這是我做了什麼:

在index.js(爲的WebPack將其導入到的WebPack束):

window.bs = require('react-bootstrap') 
window.timeago = require('react-timeago') 

在實際component.rb我有這樣的:

class Rb < React::NativeLibrary 
    imports 'bs' 
end 

class TimeAgo < React::NativeLibrary 
    imports 'timeago' 
end 

然後引用引導部件完美地工作:

Rb.Button(bsStyle: :primary) <- works as expected 

,但我不設法得到什麼的TIMEAGO包裝的:

TimeAgo.new(date: "Aug 29, 2014") {} <- just does nothing 
TimeAgo(date: "Aug 29, 2014") {}  <- method undefined 

我在做什麼錯?所有幫助讚賞!

+0

嘗試TimeAgo :: TimeAgo(date:「2014年8月29日」) –

+0

也在js控制檯中嘗試查看Opal.TimeAgo,看看裏面有什麼 –

回答

0

我發現了一個解決方法,但它並不是那麼漂亮。很想找到更好的解決方案!

class TimeAgo < React::Component::Base 
    after_mount do 
     `React.render(React.createElement(window.timeago, 
     {date: 'apr 7, 2016'}), 
     document.getElementById('something_unique') 
     );` 
    end 

    def render 
     span(id: "something_unique") { } 
    end 
end 

當然,你需要確保跨度的ID是唯一的,你通過的日期爲道具,但我離開是出於對簡潔。

+0

使用react.rb作者現在有一個修復公關爲此,這將在未來的寶石版本工作。發生這種情況時,我會在這裏更新。 – BarrieH

相關問題