Привет, ребята, я пытаюсь использовать ajax
с JSX
, можно использовать .append()
в DOM?
Пример кода:
var React = require('react');
var t = require('./../../../translations/translate');
var LocationComponent = React.createClass({
componentDidMount: function () {
$(document).ready(function() {
$.ajax({
type: 'GET',
url: 'www.api.com',
data: {},
dataType: 'json',
success: function (data) {
console.log(url);
$.each(data, function (index, element) {
// se serve il loop
var HTML = element.name;
$('#div').append(HTML);
});
}
});
});
},
render: function(){
return(
<div id="div"></div>
)
}
});
module.exports = LocationComponent;
Зачем ты это делаешь в Реакт?
Разве это не чище?
class Test extends React.Component {
constructor(props){
super(props);
this.state = {
isLoading: true,
data: []
}
}
componentDidMount(){
fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseJson) => {
this.setState({data: responseJson.movies, isLoading: false});
})
.catch((error) => {
console.error(error);
});
}
render(){
if(this.state.isLoading){
return (<div>Loading...</div>)
}
return (
<div>
{this.state.data.map(item => <div>{item.title}</div>)}
</div>
)
}
}
React.render(<Test />, document.getElementById('container'));
jQuery
только для его модуля AJAX. Загляните в специализированный HTTP-клиент, такой какaxios