小编Töl*_*ölz的帖子

Apollo Client 从缓存中删除项目

Hy 我在 React 中使用 Apollo Client。我用许多不同的变量查询帖子。所以我在不同的“缓存”中有一篇文章。现在我想删除一个帖子。所以我需要从所有“缓存”中删除这个特定的帖子。

const client = new ApolloClient({
    link: errorLink.concat(authLink.concat(httpLink)),
    cache: new InMemoryCache()
});
Run Code Online (Sandbox Code Playgroud)

后查询:

export const POSTS = gql`
    query posts(
        $after: String
        $orderBy: PostOrderByInput
        $tags: JSONObject
        $search: String
        $orderByTime: Int
    ) {
        posts(
            after: $after
            orderBy: $orderBy
            tags: $tags
            search: $search
            orderByTime: $orderByTime
        ) {
            id
            title
            ...
        }
    }
`;
Run Code Online (Sandbox Code Playgroud)

我尝试使用 cache.modify(),它在我的突变中未定义([https://www.apollographql.com/docs/react/caching/cache-interaction/#cachemodify][1])

const [deletePost] = useMutation(DELETE_POST, {
        onError: (er) => {
            console.log(er);
        },
        update(cache, data) {
            console.log(cache.modify())//UNDEFINED!!!
            cache.modify({
                id: cache.identify(thread), //identify is UNDEFINED + …
Run Code Online (Sandbox Code Playgroud)

reactjs apollo-client

8
推荐指数
2
解决办法
7152
查看次数

打字稿 useState:带参数的 Child 中的 SetState

感谢您的帮助。

如何将 setState 传递给子组件并使用 c 参数而不会出现打字稿错误?

父级:传递 setState

export interface State {
    value1: string;
    value2: string;
}

const Parent = () => {
    const [state, setState] = useState<State>({
        value1: "test",
        value2: "test",
    });

    return (
        <React.Fragment>
            <Child setState={setState} />
        </React.Fragment>
    );
};

export default Parent;
Run Code Online (Sandbox Code Playgroud)

子级:使用 setState 和 c 作为参数,c 被读取并带有类型错误下划线

type Props = {
    setState: Dispatch<State>;
};

const Child: React.FC<Props> = ({ setState }) => {
    return (
        <React.Fragment>
            <button
                onClick={() => {
                    //c is read underlined: Argument of …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs

1
推荐指数
2
解决办法
1838
查看次数

标签 统计

reactjs ×2

apollo-client ×1

typescript ×1