【发布时间】:2015-11-30 09:10:43
【问题描述】:
我从新的 React 0.14.x 收到此错误:
Warning: ReactDOMComponent: Do not access .props of a DOM node; instead, recreate the props as `render` did originally or read the DOM properties/attributes directly from this node (e.g., this.refs.box.className).
it('allows for FluxComponents through the tree via context', () => {
const flux = new Flux();
const actions = flux.getActions('test');
class TopView extends React.Component {
render() {
return (
<FluxComponent flux={flux}>
<SubView />
</FluxComponent>
);
}
}
class SubView extends React.Component {
render() {
return <SubSubView />;
}
}
class SubSubView extends React.Component {
render() {
return (
<FluxComponent connectToStores="test">
<div />
</FluxComponent>
);
}
}
const tree = TestUtils.renderIntoDocument(
<TopView />
);
const div = TestUtils.findRenderedDOMComponentWithTag(tree, 'div');
actions.getSomething('something good');
expect(div.props.something).to.equal('something good');
});
在我的情况下获取道具和上下文的正确方法是什么?
组件看起来像:
import React from 'react';
import { instanceMethods, staticProperties } from './reactComponentMethods';
import assign from 'object-assign';
class FluxComponent extends React.Component {
constructor(props, context) {
super(props, context);
this.initialize();
this.state = this.connectToStores(props.connectToStores, props.stateGetter);
this.wrapChild = this.wrapChild.bind(this);
}
wrapChild(child) {
return React.cloneElement(
child,
this.getChildProps()
);
}
getChildProps() {
const {
children,
render,
connectToStores,
stateGetter,
flux,
...extraProps } = this.props;
return assign(
{ flux: this.getFlux() },
this.state,
extraProps
);
}
render() {
let { children, render: internalRender } = this.props;
if (typeof internalRender === 'function') {
return internalRender(this.getChildProps(), this.getFlux());
}
if (!children) return null;
if (!Array.isArray(children)) {
const child = children;
return this.wrapChild(child);
} else {
return <span>{React.Children.map(children, this.wrapChild)}</span>;
}
}
}
assign(
FluxComponent.prototype,
instanceMethods
);
assign(FluxComponent, staticProperties);
export default FluxComponent;
【问题讨论】:
-
你的组件是什么样子的?在此处粘贴代码。
-
@AtanasKorchev 添加了要点
-
你确定这是产生警告的测试吗?您似乎没有在测试和组件中使用任何 DOM 元素道具。
-
@AtanasKorchev 哎呀我的错,你说得对,更新了测试规范要点
标签: javascript dom reactjs tdd flux