JSONãã¼ã¿ãReactã§ãªã¹ã表示ãã
åä¸ã³ã³ãã¼ãã³ã
this.setState
ã¡ã½ããã§stateã¨ãã¦åå¾
componentDidMount() { $.ajax({ url: this.props.url, data: { q: "select title from feed where url = '" + this.props.target + "'", format: "json" }, type: 'GET', dataType: 'json', success: function(res) { for (var i in res.query.results.entry) { console.log(res.query.results.entry[i].title); } this.setState({entry: res.query.results.entry}); }.bind(this), error: function(xhr, status, err) { console.log(this.props.targetURL, status, err.toString()); }.bind(this) }); }
ãã®åæstateã¯é åã«ãã¦ãã
class FeedTitle extends React.Component { constructor() { super(); this.state = ({ entry: [] }); }
mapã¡ã½ããã使ã£ã¦stateããªã¹ã表示ããã®éãã¦ãã¼ã¯ãªkeyã渡ãã
render() { return( <ol> {this.state.entry.map(function(index) { return <li key={index.title}>{index.title}</li>; })} </ol> ); } } ReactDOM.render ( <FeedlTitle url="http://query.yahooapis.com/v1/public/yql?callback=?" target="http://uraway.hatenablog.com/feed" />, document.getElementById('content') );
ã½ã¼ã¹ã³ã¼ã
class FeedlTitle extends React.Component { constructor() { super(); this.state = ({ entry: [] }); } componentDidMount() { $.ajax({ url: this.props.url, data: { q: "select title from feed where url = '" + this.props.target + "'", format: "json" }, type: 'GET', dataType: 'json', success: function(res) { for (var i in res.query.results.entry) { console.log(res.query.results.entry[i].title); } this.setState({entry: res.query.results.entry}); }.bind(this), error: function(xhr, status, err) { console.log(this.props.targetURL, status, err.toString()); }.bind(this) }); } render() { return( <ol> {this.state.entry.map(function(index) { return <li key={index.title}>{index.title}</li>; })} </ol> ); } } ReactDOM.render ( <FeedlTitle url="http://query.yahooapis.com/v1/public/yql?callback=?" target="http://uraway.hatenablog.com/feed" />, document.getElementById('content') );
è¤æ°ã³ã³ãã¼ãã³ã
ã³ã³ãã¼ãã³ããè¤æ°ä½ãæãkeyã¯å å ãããHTMLã§ã¯ãªããã³ã³ãã¼ãã³ãã«ç´æ¥æ¸¡ããã¨ã«æ³¨æããã
class FeedTitle extends React.Component { ~~~~~~ render() { return( <ul> {this.state.entry.map(function(index) { return <TitleList key={index.title} index={index} />; })} </ul> ); } } class TitleList extends React.Component { render() { <ol>{this.props.index.title}</ol>; } }
keyã«ã¤ãã¦ã¯ãã¡ãKeyed Fragmentsãèªãã¨ãã£ã¨é¢ç½ãã