Fetching Tweets
Now that we have added the logics to create a new tweet, it's time to fetch all the tweets in the UI list we created earlier:
Fetching tweets
To fetch tweets, we can simply use the findPosts method of the api.
Example:
const spaceIdForTweets = '1059'
const tweetIds = await api.blockchain.postIdsBySpaceId(spaceIdForTweets)
const tweets = await api.base.findPosts({ids: tweetIds})
Now, lets put this logic inside a useEffect hook on the home.tsx file.
const HomeScreen = () => {
// ...
const [tweets, setTweets] = useState<ITweet[]>([])
const { api, isReady } = useContext(SubsocialContext)
const fetchTweets = async () => {
if (!api) return;
const spaceIdForTweets = '1059'
const tweetIds = await api.blockchain.postIdsBySpaceId(spaceIdForTweets)
const tweets = await api.base.findPosts({ ids: tweetIds })
console.log(tweets)
setTweets(tweets.map((tw) => {
const t: ITweet = {}
t.id = tw.struct.id.toString();
t.description = tw.content?.body;
t.name = tw.content?.title;
t.avatar = 'https://i.pravatar.cc/300';
t.likes = tw.struct.upvotesCount.toNumber();
return t;
}))
}
useEffect(() => {
fetchTweets()
}, [isReady])
// ...
}