出于某种原因,我的部分 React 组件正在渲染为字符串。
我将 Rails 与gem 一起使用react-rails
,而sprockets-coffee-react
gem 将 JSX 与 Coffeescript 一起使用。
这是代码
显示.html.erb
<%= react_component 'ArticleForm', {action: articles_path,
authenticity_token: form_authenticity_token,
collection_id: @collection.id,
} %>
article_form.js.coffee.cjsx
window.ArticleForm = React.createClass
getInitialState: ->
metadata: {}
url: ''
handleChange: ->
console.log('test')
render: ->
<form accept-charset="UTF-8" action={this.props.action} method="post">
<AuthenticityTokenInput authenticity_token={this.props.authenticity_token} />
</form>
authentity_token_input.js.coffee.cjsx
window.AuthenticityTokenInput = React.createClass
render: ->
<div style={{margin: 0, padding: 0, padding: 'inline'}}>
<input name="utf8" type="hidden" value="✓">
<input name="authenticity_token" type="hidden" value={this.props.authenticity_token}>
</div>
和输出:
<input name="utf8" type="hidden" value="✓"> <input name="authenticity_token" type="hidden" value=zlZevmHku5ZU+UA998KwmJHAMn94uVkilrBj1iC/A8o=>
由于某种原因,它将两个输入呈现为纯文本而不是实际的 html 标签。
当我检查元素时,这就是我得到的:
<form action="/articles" method="post" data-reactid=".0">
<div style="margin:0;padding:inline;" data-reactid=".0.0">
<span data-reactid=".0.0.0"><input name="utf8" type="hidden" value="✓">
<input name="authenticity_token" type="hidden" value=</span>
<span data-reactid=".0.0.1">zlZevmHku5ZU+UA998KwmJHAMn94uVkilrBj1iC/A8o=</span>
<span data-reactid=".0.0.2">></span>
</div>
</form>