Io sto cercando di fare il tutorial: https://facebook.github.io/react/docs/tutorial.htmlCome TypeError: this.props non è definito sulla ReactJs
import React, { PropTypes } from 'react';
import classnames from 'classnames';
import styles from './CommentBox.css';
import withStyles from '../../decorators/withStyles';
import Link from '../../utils/Link';
import $ from 'jquery';
@withStyles(styles)
class CommentBox extends React.Component {
constructor() {
super();
this.state = {data: []};
}
loadCommentsFromServer() {
$.ajax({
url: this.props.url,
dataType: 'json',
cache: false,
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
})
}
componentDidMount() {
this.loadCommentsFromServer();
setInterval(this.loadCommentsFromServer, this.props.pollInterval);
}
render() {
let url="/public/comments.json"
return (
<div className="commentBox">
<h1>Comments</h1>
<CommentList data={this.state.data} />
<CommentForm />
</div>
);
}
}
class CommentList extends React.Component {
render() {
let data = this.props.data
var commentNodes = data.map(function (comment) {
return (
<Comment author={comment.author}>
{comment.text}
</Comment>
);
});
return (
<div className="commentList">
{commentNodes}
</div>
);
}
};
class Comment extends React.Component {
render() {
return(
<div className="comment">
<h2 className="commentAuthor">
{this.props.author}
</h2>
{this.props.children}
</div>
);
}
}
class CommentForm extends React.Component {
render() {
return (
<div className="commentForm">
Hello, world! I am a CommentForm.
</div>
);
}
};
export default CommentBox;
Tuttavia, il tutorial è un po 'obsolete e sto usando React 0.14-rc1 con la sintassi ES6. Ho fatto del mio meglio per seguire il tutorial e implementarlo nel modo 0.14. È stato in grado di arrivare a questo punto, ma ora ricevendo l'errore:
TypeError: this.props is undefined
Impossibile risolvere il problema. Qualche idea del perché? Grazie
È possibile fornire la posizione dell'errore nel documento per capire il problema? –
url: this.props.url, – hilarl
Puoi aggiungere questa parte di codice _static defaultProps = { // i tuoi oggetti di scena qui: valore predefinito } _ nella tua classe ComentBox, anche [qui] (http://babeljs.io/blog/2015/06/07/react-on-es6-plus /) un buon esempio di come utilizzare ** React ** e ** ES6 ** –