-1
我已經建立了一個應用程序與此類ReactDOM.render陣營AJAX數據集成
ReactDOM.render(
<Carousel>
<img src="http://placehold.it/300x100" short="test test test test test test test test test test test test" long="1Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quasi, quam modi dolore, reiciendis fugit illo vel? Dolorum qui ad pariatur non eaque consequatur illum inventore, unde repudiandae, possimus eum vitae."/>
<img src="http://placehold.it/300x100" short="test test test test test test test test test test test test" long="1Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quasi, quam modi dolore, reiciendis fugit illo vel? Dolorum qui ad pariatur non eaque consequatur illum inventore, unde repudiandae, possimus eum vitae."/>
</Carousel>,
document.getElementById('contentModule')
);
現在我不得不讓IMG是一個單一的元素,這與JSON整合。
[
{
"src":"\/media\/images\/test\/corporate_portfolio.jpg",
"short":"test test test test test test test test test test test test",
"long":"0Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quasi, quam modi dolore, reiciendis fugit illo vel? Dolorum qui ad pariatur non eaque consequatur illum inventore, unde repudiandae, possimus eum vitae.",
},
{
"src":"\/media\/images\/test\/corporate_portfolio.jpg",
"short":"test test test test test test test test test test test test",
"long":"1Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quasi, quam modi dolore, reiciendis fugit illo vel? Dolorum qui ad pariatur non eaque consequatur illum inventore, unde repudiandae, possimus eum vitae.",
},
]
有沒有辦法與它的孩子使用json構建一個Carousel?或者我必須改變我的代碼,以其他方式合併它?
我嘗試加載數據並將其分配給狀態,但我無法確定是否可以在我的私有渲染函數中使用此對象。
var Carousel = React.createClass({
componentDidMount: function() {
this._loadData();
},
render: function(){
let style = this._handleHeight();
return (
<div
className='carousel' style={style}>
<div className="figure-wrapper">
{this._renderFigureNodes()}
</div>
</div>
)
},
_loadData: function() {
$.ajax({
url: '/misc/bundles.php',
dataType: 'json',
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
console.error('#GET Error', status, err.toString());
}.bind(this)
});
},
_renderFigureNodes() {
let figureNodes = React.Children.map(this.props.children, (child, index) => {
let style = this._handleFigureStyle(index, this.state.current);
let className = this._handleFigureClass(index, this.state.current);
let current = this.state.current;
return (
<figure
className={className}
style={style}
key={index}
onClick={this._handleFigureClick.bind(this, index, current)}
onMouseEnter={this._handleFigureHover.bind(this, index, true)}
onMouseLeave={this._handleFigureHover.bind(this, index, false)}
>
{child}
<div className='short'>
{child.props.short}
</div>
</figure>
);
});
return figureNodes;
}
});
我需要以某種方式集成{child}和{child.short}與json。
我試圖在_renderFigureNodes中映射我的數據對象,但它調用函數時似乎未定義。
this.state.data.map(function (data) {
console.log(data.short)
})