将this example升级到React.js 0.14 .....
切换时收到错误:
未捕获的TypeError:无法读取属性' setState'为null
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import classNames from 'classnames';
import { Link } from 'react-router';
export default class TreeNode extends Component {
constructor(props) {
super(props);
this.state = {
visible: true
};
}
render() {
let childNodes;
let classObj;
if (this.props.node.childNodes !== undefined) {
childNodes = this.props.node.childNodes.map((node, index) => {
return (
<li key={ index }><TreeNode node={ node } /></li>
);
});
}
classObj = classNames({
'togglable': true,
'togglable-down': this.state.visible,
'togglable-up': !this.state.visible
});
let style;
if (!this.state.visible) {
style = { display: 'none' };
}
return (
<div>
<h5 onClick={ this.toggle } className={ classObj }>
{ this.props.node.title }
</h5>
<ul style={ style } >
{ childNodes }
</ul>
</div>
);
}
toggle() {
this.setState({ visible: !this.state.visible });
}
}
TreeNode.propTypes = {
node: React.PropTypes.object
};
ReactDOM.render(<TreeNode node={ treeData } />, document.querySelector('#reactRoot'));
有人看到我的错误吗?
如果你能指出的话,非常感谢。
答案 0 :(得分:0)
没关系,愚蠢,愚蠢,愚蠢......
<h5 onClick={ this.toggle.bind(this) } className={ classObj }>