React-rails:映射多个道具
React-rails : Map multiple props
本文关键字:映射 React-rails 更新时间:2023-09-26
我刚刚开始与react-rails gem,这里是我下面的代码,我需要帮助。我的_mounts.js.jsx
var Mounts = React.createClass({
render () {
var showMounts = (mount, details) => <Mount name={mount.name} path={mount.mount_point} det={details} />;
return (
<div className="card">
<div className="card-main">
<div className="card-inner margin-bottom-no">
<p className="card-heading">Mount Points</p>
<div className="card-table">
<div className="table-responsive">
<table className="table table-hover table-stripe" >
<thead>
<tr>
<th>Mounted as</th>
<th>Path</th>
<th>Size</th>
<th>Mount/Unmount</th>
</tr>
</thead>
<tbody>
{this.props.mounts.map(showMounts}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
)
}
});
我在我的react_component中传递了两个道具,
<%= react_component "Mounts", { mounts: @mounts, mount_details: b} %>
@mounts是一个文件系统对象,b是一个数组。
我_mount.js。jsx文件
var Mount = React.createClass({
render () {
var showMount = (mount) => <Mount name={mount} />;
if(this.props.det == "true"){
dat = "Unmount"
}
else{
dat = "Mount"
}
return (
<tr>
<td> {this.props.name}</td>
<td> {this.props.path}</td>
<td> Undefined Size</td>
<td>
{this.props.det}
</td>
</tr>
)
}
});
My {this.props.det}显示的是数组索引值,而不是索引中的值,即(0,1,2,3)。
我是React的新手,任何帮助都很感激。
实际上,这是预期的行为。对于传递给Array.prototype.map
的函数,第二个参数将是索引。
下面是一个例子:
var myArray = ["A", "B", "C"]
var myMapFn = function(item, index) {
console.log(index, " => ", item)
}
myArray.map(myMapFn)
// 0 => A
// 1 => B
// 2 => C
注意myMapFn
的参数是(item, index)
。
我认为您需要mount_details
数组中的项而不是该索引。对吗?
如果是这样,您可以通过修改showMounts
:
var mountDetails = this.props.mount_details
var showMounts = function(mount, mountIndex) {
// use the given index to find the matching `details` object:
var details = mountDetails[mountIndex]
// now create the <Mount />
return (
<Mount
name={mount.name}
path={mount.mount_point}
det={details}
/>
)
}
在该实现中,函数使用给定的索引从mountDetails
数组中查找相应的项。
那是你想要的吗?
相关文章:
- React Rails应用程序中动态子项的密钥分配问题
- 从映射返回React渲染数组
- 为什么我不能将键映射到 React 组件上
- React Native数组映射与多维数组
- Ember.js+传单+rails(使用Javascript MVC创建rails应用程序并打开源代码映射)
- 当数据准备就绪时,React-Rails gem 调用组件
- 通过带有对象子级的数组进行 React 本机映射 - 工作方式与在 react web 中不同
- Webpack 模块构建失败意外令牌(rails react build)
- 在 React.js 中映射时引用道具时遇到问题
- react js和rails在具有活动记录关系的组件上更新状态
- 如何使用react rails gem预呈现一个名称空间的react组件
- React/Redux:mapStateToProps实际上并没有将状态映射到道具
- React-rails:映射多个道具
- 使用react-rails gem移除rails中的javascript渲染阻塞
- React Rails-开始时出现的问题
- 如何在Rails路由器之上使用React Router(不使用React - Rails gem)
- 如何在react rails中使用mobx
- React-Rails:如何使用ref渲染组件
- React Rails组件:手动触发重新渲染
- ExecJS::RuntimeError Rails 4 using react-rails gem