[英]Material-UI component not being rendered with react-rails prerender
I'm trying to include a react component dynamically using javascript ie a user can click "Add Place" and a form gets added to the page. 我正在尝试使用javascript动态包含一个react组件,即用户可以单击“添加位置”,然后将表单添加到页面。 This form contains a react component that uses material-ui's auto-complete component. 该表单包含一个使用material-ui的自动完成组件的react组件。
Below is the code for the form that gets added dynamically when button's clicked: 以下是单击按钮时动态添加的表单代码:
<div class="grid-x grid-margin-x grid-margin-y grid-padding-x grid-padding-y align-center">
<div class="cell medium-6">
<div class="field">
<%= f.label :activity_place %><br/>
<%= react_component("PlaceSearch", {}, {prerender: true}) %>
</div>
</div>
<div class="cell medium-6">
<div class="field">
<%= f.label :activity_time %><br/>
<%= f.select(:activity_time, Constants::ACTIVITY_TIMES) %>
</div>
</div>
</div>
Below is the code for the Component (excluding imports): 以下是该组件的代码(不包括导入):
class PlaceSearch extends React.Component {
state = {
checked: false,
dataSource: [
{
text: 'text-value1',
value: (
<MenuItem
primaryText="text-value1"
secondaryText="☺"
/>
),
},
{
text: 'text-value2',
value: (
<MenuItem
primaryText="text-value2"
secondaryText="☺"
/>
),
},
]
}
render () {
return (
<MuiThemeProvider muiTheme={getMuiTheme({userAgent: (typeof navigator !== 'undefined' && navigator.userAgent) || 'all' })}>
<div>
<AutoComplete
hintText="text-value data"
filter={AutoComplete.noFilter}
dataSource={this.state.dataSource}
/>
</div>
</MuiThemeProvider>
);
}
}
export default PlaceSearch
This example I've picked from material-ui's docs. 我从material-ui的文档中选择了这个示例。
Everything works fine as expected except the react component. 除了react组件之外,其他所有组件都按预期工作。 If I try to add the react component inclusion code somewhere else apart from the form being added dynamically, it works as expected. 如果我尝试将react组件包含代码添加到动态添加的表单之外的其他位置,则它将按预期工作。
I've searched different forums regarding material-ui and rails but could not find the solution to this particular problem. 我搜索了有关Material-ui和Rails的不同论坛,但找不到针对此特定问题的解决方案。
You should put your state declaration inside a constructor: 您应该将状态声明放入构造函数中:
class PlaceSearch extends React.Component {
constructor() {
super();
this.state = {
checked: false,
dataSource: [
{
text: 'text-value1',
value: (
<MenuItem
primaryText="text-value1"
secondaryText="☺"
/>
),
},
{
text: 'text-value2',
value: (
<MenuItem
primaryText="text-value2"
secondaryText="☺"
/>
),
}]
}
}
render () {
return (
<MuiThemeProvider muiTheme={getMuiTheme({userAgent: (typeof navigator !== 'undefined' && navigator.userAgent) || 'all' })}>
<div>
<AutoComplete
hintText="text-value data"
filter={AutoComplete.noFilter}
dataSource={this.state.dataSource}
/>
</div>
</MuiThemeProvider>
); } }
export default PlaceSearch
If this works for you, this is why: SO Post 如果这对您有用 ,这就是原因: SO Post
Finally made it working by adding manual component mounting code ie ReactRailsUJS.mountComponents() as mentioned in the github doc of the react-rails gem. 最后,通过添加手动组件安装代码(即ReactRailsUJS.mountComponents())使其工作,如react-rails gem的github文档中所述。 Did no changes to the component JS file but only to the form that included it. 没有更改组件JS文件,仅更改了包含它的表单。 The working code is below: 工作代码如下:
<div class="grid-x grid-margin-x grid-margin-y grid-padding-x grid-padding-y align-center">
<div class="cell medium-6">
<div class="field">
<%= f.label :activity_place %><br/>
<%= react_component("PlaceSearch", {}, {prerender: true}) %>
</div>
</div>
<div class="cell medium-6">
<div class="field">
<%= f.label :activity_time %><br/>
<%= f.select(:activity_time, Constants::ACTIVITY_TIMES, include_blank: true) %>
</div>
</div>
</div>
<script>
ReactRailsUJS.mountComponents()
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.