ReactJS高阶组件在渲染循环中不转发属性
ReactJS high order components not forwarding property in a render loop
我正在寻找一些指导。我遵循React拖放象棋教程,将相同的原则应用到我的项目中。但是当子类需要递归重复时,我陷入了困境。当Child从Parent渲染时,一切都工作了。但是,当我递归地从一个子渲染函数呈现一个子,我得到关于丢失默认道具的错误,应该由DragSource()根据高阶组件的工作方式转发。我将在代码下面给出props的控制台输出。
在下面的代码中是我试图做的一个简化版本:
Constants.js
export const ItemTypes = {
CHILD: 'child'
}
Parent.jsx
import React, { PropTypes } from 'react'
import Child from './Child'
import { DragDropContext } from 'react-dnd'
import HTML5Backend from 'react-dnd/modules/backends/HTML5'
class Parent extends React.Component {
render () {
return (
<Child />
)
}
}
export default DragDropContext(HTML5Backend)(Parent)
Child.jsx
import React, { PropTypes } from 'react'
import { ItemTypes } from './Constants'
import { DragSource } from 'react-dnd'
const taskSource = {
beginDrag (props) {
return {
index: props.index
}
}
}
function collect (connect, monitor) {
return {
connectDragSource: connect.dragSource(),
isDragging: monitor.isDragging()
}
}
class Child extends React.Component {
static get propTypes () {
return {
index: PropTypes.number,
connectDragSource: PropTypes.func.isRequired,
isDragging: PropTypes.bool.isRequired
}
}
constructor(props) {
super(props)
console.log(props)
this.state = {
children: [1, 2, 3, 4]
}
}
render () {
const { connectDragSource, isDragging } = this.props
return connectDragSource(
<ul className='children'>
{ this.state.children.map((child, i) =>
<li key={child}>
<Child index={i} ref={'child/' + i} />
</li>
) }
</ul>
)
}
}
export default DragSource(ItemTypes.CHILD, taskSource, collect)(Child)
从父组件渲染函数调用子组件道具时的Console.log输出
{
_id: "123"",
connectDragSource: fn(),
isDragging: false
}
从子组件呈现函数递归调用子组件道具时的Console.log输出
{
_id: "1234"
index: 0
}
ERROR: Uncaught (in promise) TypeError: connectDragSource is not a function
所以基本上我认为发生的事情是高阶组件没有将默认道具转发给从子组件循环呈现的子组件。
我希望任何人都能给我一些建议,因为我已经研究了几个小时了。谢谢。所以这实际上是我学到的一个非常简单的答案。Parent中导入的子节点。jsx与child .jsx中的子节点不同。parent中的Child类具有高阶组件的行为,而对其自身的子引用尚未应用高阶组件。为了解决这个问题,我必须在Child.jsx:
中这样做render () {
const { connectDragSource, isDragging } = this.props
return connectDragSource(
<ul className='children'>
{ this.state.children.map((child, i) =>
<li key={child}>
<HigherChild index={i} ref={'child/' + i} />
</li>
) }
</ul>
)
}
const HigherChild = DragSource(ItemTypes.CHILD, taskSource, collect)(Child)
export default HigherChild
相关文章:
- RegEx删除空属性?例如,如果(class=“”||class=“”)移除;否则就下课
- 全局变量和全局对象的属性之间有什么区别吗
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- 序列化数据属性中对象的最可靠方法
- 分析高度属性时出现意外值{{specs.height}}.index.html
- TypeError:无法读取属性'推'未定义的JavaScript
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- AngularJS-使用'true'属性
- 可以't使用JavaScript获取width属性
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 如何使用javascript获取嵌套对象中所有子对象的单个属性
- JavaScript Pub/Sub属性访问问题
- 从JavaScript访问struts操作中的属性
- 是否可以从父类访问子类的属性
- 如何更改reactjs中外部/独立组件的状态或属性
- 如何在选项卡上定义属性'的主窗口对象
- 锚点元素的href属性自动更改
- 当使用jQuery单击时,如何转发到html中数据属性中的链接
- ReactJS高阶组件在渲染循环中不转发属性