如何与已登录用户的内容分享在社交媒体上?

huangapple go评论59阅读模式
英文:

How to share on social media with logged user content?

问题

I'm new using React, so please excuse me for the basic question. I didn't know where to find the right answer (or even how to ask the proper question). My doubt is: I have a React app where the user will have his content, which is basically a text and an image (using base64). He must be logged in to access it. I'm saving the content inside Firebase, as a JSON file with logged user.id generated by Firebase, as the category, and each user content is composed of an UUID I generate, the image (base64), and a text.

So, main page I list all the content for each logged user, and I want to put a button there so (s)he can share his content on Facebook, for instance. This "content" I'm calling a user story. So, for each listed story I create a Link to load this particular story in another page:

arrayOfStories.map((story) => (
    (...)
    <Link className="logo-container" to='/loadedstory' state={story}>
))

I'm passing the story object, which is basically the JSON with storyText, image, and uuid properties in a useLocation() state.

Then, from my loadedstory.component.jsx context page, I do this:

const LoadedStory = () => {
    const {state} = useLocation();

    (...)

    return (
        <>
            <div>
                { state && state.uuid ? (
                    <div className="story-content"><img alt="Your content!" src={`data:image/png;base64,${state.image}`} className='resize-image'></img></div>
                    <div className="story-content"><p><span className='stroke'>{state.storyText}</span></p></div>
                    (...)
                ): 
                (<div></div>)}
            </div>
            (...)
        </>
    );
}

As you can see, if the user wants to share this particular story on Facebook, the base URL link will be /loadedstory and won't load anything without the logged user context from useLocation().

What's the best practices for developing such shareable content within a logged context?

I was thinking about doing something like /loadedstory/:uuid to use the story UUID from each story and query Firebase every time this context is needed, but the problem is my Firebase category is the user.id not the story.uuid. Would I need to query the whole users in the database then for this UUID? Is this the right approach?

英文:

I'm new using React, so please excuse me for the basic question. I didn't know where to find the right answer (or even how to ask the proper question). My doubt is: I have a React app where the user will have his content, which is basically a text and an image (using base64). He must be logged in to access it. I'm saving the content inside Firebase, as a JSON file with logged user.id generated by Firebase, as the category, and each user content is composed of an UUID I generate, the image (base64), and a text.

So, main page I list all the content for each logged user, and I want to put a button there so (s)he can share his content on Facebook, for instance. This "content" I'm calling a user story. So, for each listed story I create a Link to load this particular story in another page:

arrayOfStories.map((story) =&gt; (

    (...)
    &lt;Link className=&quot;logo-container&quot; to=&#39;/loadedstory&#39; state={story}&gt;

))

I'm passing the story object, which is basically the JSON with storyText, image, and uuid properties in a useLocation() state.

Then, from my loadedstory.component.jsx context page, I do this:



const LoadedStory = () =&gt; {
    const {state} = useLocation();

(...)

return (
    &lt;&gt;
        &lt;div&gt;
            
            { state &amp;&amp; state.uuid ? (
              &lt;div className=&quot;story-content&quot;&gt;&lt;img alt=&quot;Your content!&quot; src={`data:image/png;base64,${state.image}`} className=&#39;resize-image&#39;&gt;&lt;/img&gt;&lt;/div&gt;

&lt;div className=&quot;story-content&quot;&gt;&lt;p&gt;&lt;span className=&#39;stroke&#39;&gt;{state.storyText}&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;

(...)

             ): 
            (&lt;div&gt;&lt;/div&gt;)}

        &lt;/div&gt; 
(...)

    &lt;/&gt;

As you can see, if the user wants to share this particular story on Facebook, the base URL link will be /loadedstory and won't load anything without the logged user context from useLocation().

What's the best practices for developing such shareable content within a logged context?

I was thinking about to do something like /loadedstory/:uuid to use the story UUID from each story and query Firebase everytime this context is needed, but the problem is my Firebase category is the user.id not the story.uuid. Would I need to query the whole users in the database then for this UUID? Is this the right approach?

答案1

得分: 1

如果你正在使用NextJS,你可能可以保持数据不变,并为每个用户及其所有故事生成一个静态页面,以及指向这些故事的页面。

你可以将用户ID和故事ID传递给故事页面。这样,故事可以获取所有用户的数据并对其进行筛选以找到故事ID。如果你使用类似ReactQuery的东西,它会进行缓存,所以你不会感到太糟糕地请求已经拥有的数据。

至于“最佳实践”,通常情况下,你会创建一个新的数据库表,将每个故事存储为自己的记录,并将用户ID作为所有者。你可以使用类似GraphQL的东西来仅获取用户、仅获取故事或获取用户及其前25个故事。

你还需要一个“路由器”来促进导航(页面或内容)而不需要重新发明轮子。

一个非常流行的解决方案是使用NextJS构建应用程序。它很容易上手,它们的入门示例包括导航。

许多项目使用React Router v6,无论你最终是否使用它,都值得浏览一下他们的教程

如果你想要额外的类型安全性,可以查看TanStack Router。它是基于许多先前的伟大想法构建的新项目。

英文:

If you 're using NextJS, you could probably keep the data as-is and generate a static page for every user and all their stories, and the pages pointing to those stories.

You could pass the user-id and story-id to the story page. That way the story can fetch the (all) the user's data and sift through it to find the story-id. If you use something like ReactQuery, it will cache so you won't feel so bad about asking for data you already have.

As far as 'best practice', generally you would make a new DB table to store each story as its own record with the user-id as an owner. You use something like GraphQL to fetch only a user, only a story, or a user and their first 25 stories.

You also need a "router" to facilitate navigating around (pages or content) without re-inventing the wheel.

One very popular solution is to build the app using NextJS. It's easy to get up and running and their starter example covers navigation.

Many projects use React Router v6 and it's well worth running through their tutorial whether you'll end up using it or not.

If you want extra type safety, check out the TanStack Router. It's new and built on the shoulders of many great ideas that preceded it.

huangapple
  • 本文由 发表于 2023年2月14日 03:08:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定