2
I am doubtful about the GET function of my JS (React JS) code, I have the GET function using the Fetch api, it returns the values a certain link.json containing an array. Using this link as an example: http://jsonplaceholder.typicode.com/posts - I want to take the contents of the first 'Id'(1) and put in one part of the code, and also another 'Id' (Example: 5) any of the array to another part of the code.
constructor(props) {
super(props);
this.state = {
data:[]
}
}
componentDidMount(){
let URL = 'http://jsonplaceholder.typicode.com/posts'
fetch(URL)
.then(function(response) {
let data = response.json()
return data;
})
.then((json) => {
console.log('mensagens: ', json)
this.setState({data : json});
})
.catch(function(ex) {
console.log('parsing failed', ex)
})
}
UPDATING:
{
this.state.data.map(obj => (
if(obj.displayPortraitLeft == true){
<div className="box-body">
<div className="direct-chat-msg undefined">
<div className="direct-chat-info clearfix">
<span className="direct-chat-name pull-right">{obj.userName}</span>
<span className="direct-chat-timestamp pull-left">{obj.time}</span>
</div>
</div>
</div>
}else{
<div className="box-body">
<div className="direct-chat-msg undefined">
<div className="direct-chat-info clearfix">
<span className="direct-chat-name pull-right">{obj.userName}</span>
<span className="direct-chat-timestamp pull-left">{obj.time}</span>
</div>
</div>
</div>
))
}
Returns in browser : . /src/chat/Chat.js Syntax error: C:/Users/Henri/Desktop/funcionando/my-app/src/chat/Chat.js: Unexpected token (41:19)
39 | {
40 | this.state.data.map(obj => (
> 41 | if(obj.displayPortraitLeft == true){
| ^
42 | <div>
43 | <div className="box-body">
44 |
Hi Henrique. You solved yesterday’s CSS question?
– Sergio
I got the position part of each widget in different screen sizes, like: Small screens one below the other, Medias screens, 2 columns and 2 Rows...
– Henrique Hermes
Okay, if you don’t have it all figured out, just put the rest of the HTML or JSX in there so we can help. I answered that question here, though, see if it makes sense, and that’s what you’re looking for. In the other questions I see that you are using other components, here I gave example with simple html.
– Sergio
Henrique, you can simplify your IF already line 41. If the value of obj.displayPortraitLeft is true or false, you can leave it alone
if (obj.displayPortraitLeft)
– Rafael Cavalcante