如果图片值得一千个单词,那么一个交互式演示必须值得...一百万? 您喜欢通过流行语滚动以了解应用程序的目的吗?可能不是。而且我不在乎为我的最新激情项目Wanna写所有这些泡沫。因此,我追求了一个更有趣的解决方案:将我的应用程序嵌入其自己的着陆页中供用户探索! [2
这个GIF具有263帧,所以我想它值263,000个单词执行
只需渲染我们的root App组件并将其称为一天:
演示应用程序将检索真实数据,该数据可能会失败或不显示它
对于用户所看的
export const InteractiveDemo = () => { return (让我们解决这些问题。 Wanna使用React路由器V6和Apollo GraphQl,但是这些概念都适用于技术。) }
导航
为了向演示应用提供假数据,我们使用usestate在客户端应用程序中维护一个假的“后端”,并通过模拟客户端或服务器(取决于实现)。它对其余的APP代码的侵入性最少,甚至让我们使用演示进行手动测试 - 快速迭代时非常方便。
我使用了Mock-apollo-client;对于休息或TRPC,您可以使用诸如Nock之类的东西。它们是用于自动测试的,但正是我们在这里需要的。
import { MemoryRouter, UNSAFE_LocationContext } from 'react-router' export const InteractiveDemo = () => { return ( // Hack to nest MemoryRouter inside BrowserRouter. // https://github.com/remix-run/react-router/issues/7375然后,就像我们对导航所做的一样,我们与模拟客户端一起将演示包装在新的提供商中:) }
如果您使用模拟服务器,则将其URL注入Demo App的真实客户端。
视觉效果
有用!现在,我们如何使用户显而易见他们正在查看交互式演示?
import { InMemoryCache } from '@apollo/client' import { createMockClient, createMockSubscription } from 'mock-apollo-client' import { useMemo, useState } from 'react' // GraphQL documents that our client sends to the real server import GET_FRIENDS from '../../gql/getFriends.gql' import ADD_FRIEND from '../../gql/addFriend.gql' // Simplified example export const useDemoClient = () => { const [friends, setFriends] = useState[{ __typename: 'User', id: 1, name: 'Nick', }] // Cache should persist across clients const cache = useMemo(() => { // Should be the same cache configuration you provide to your real Apollo client return new InMemoryCache() }, []) // We need to recreate the mock client whenever the data changes // because it doesn't support resetting request handlers. const mockClient = useMemo(() => { const client = createMockClient({ cache }) client.setRequestHandler(GET_FRIENDS, () => Promise.resolve({ data: { friends: friends } })) client.setRequestHandler(ADD_FRIEND, ({ user }) => { setFriends((prev) => prev.concat([user])) return Promise.resolve({ data: { addFriend: user } }) }) return client }, [friends]) return mockClient }
最后,有一个动画有帮助,因此显然不是静态图像。例如,想在活动输入字段中不断提出的幽灵类型建议。
export const InteractiveDemo = () => { return (现在我们拥有InteractiveMo组件,我们将其渲染到我们的着陆页中,我们完成了!) }
导出const landing =()=> { 返回 (
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3