-3
I would like some help: I have two Json: posts and Author. Json posts have no id. Ojson returns right in the log. I already have the card component created, but I can’t render the posts on the screen. Each post has a respective author. Grateful
Jsons : Publications: http://www.mocky.io/v2/5be5e3fa2f000082000fc3f8 Authors: http://www.mocky.io/v2/5be5e3ae2f00005b000fc3f6
App.js:
import Routes from './routes';
import api from '../src/services/api';
import Navbar from '../src/components/Navbar';
import PostCard from '../src/components/PostCard';
function App() {
const [posts, setPosts] = useState([]);
useEffect(() => {
async function loadPosts() {
const response = await api.get('5be5e3fa2f000082000fc3f8')
const { postsApi } = await response.data
console.log(response.data);
setPosts(postsApi);
}
loadPosts();
}, []);
return (
<>
<div id="App">
<Navbar></Navbar>
<section className="cards">
{posts?.map((post, index) => (
<div key={index}>
<PostCard post={post} ></PostCard>
</div>
))}
</section>
</div>
</>
);
}
export default App; ```
And what’s the problem? A
key
on the basis ofindex
?– Rafael Tavares
Does not render posts on screen..
– Dev777
Ah, yes... It is written in the question but passed me beaten. With the syntax
posts.map
(removing the question mark?
) works? I know this syntax in Kotlin but I’ve never seen in React...– Rafael Tavares
Hum.Does not work. Does not recognize the Function map if you take the ?
– Dev777
If you put a
console.log(posts)
before thereturn
, what appears on the console? Edith the question with that information.– Rafael Tavares
Returns right. As in link
– Dev777
If the variable
posts
is with the correct value (containing an array),posts.map
must work. If you put, inside themap
, tags onlyp
, for example<p>Teste</p>
, he still shows nothing?– Rafael Tavares
Still continues.
– Dev777
your doubt is how to unite these two results?
– novic
Yes, unite the two
– Dev777
So change the question and ask how I make the union that by what I can understand you can already rescue the two json.
– novic
It was giving error, but it was fixed.. But the second step would be to join the two JSON, what is still missing..
– Dev777