第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

React TypeError:無法讀取未定義的屬性“getDates”

React TypeError:無法讀取未定義的屬性“getDates”

手掌心 2021-11-18 21:18:37
今天我決定更新我的 react 項(xiàng)目的依賴項(xiàng),我的組件Home不再工作了,我實(shí)際上正在使用 apollo 客戶端和 apollo react 鉤子,這是 miHome組件文件:function Home(props) {    const {        loading,        data: { getPosts: posts }    } = useQuery(FETCH_POSTS_QUERY);    return (        <Grid columns={3} stackable={true} className={loading ? 'loading' : ''}>            <Grid.Row className='page-title'>                <h1>Recent Posts</h1>            </Grid.Row>            <Grid.Row>                {user && (                    <Grid.Column>                        <PostForm user={user} />                    </Grid.Column>                )}                {loading ? (                    <Loading />                ) : (                    posts &&                    posts.map(post=> (                        <Grid.Column key={post._id} style={{ marginBottom: 20 }}>                            <PostCard post={post} />                        </Grid.Column>                    ))                )}            </Grid.Row>        </Grid>    );}我在瀏覽器中收到此錯(cuò)誤: “TypeError:無法讀取未定義的屬性‘getPosts’”我正在嘗試用這個(gè)小小的代碼變體來修復(fù)它:function Home(props){    let posts = '';    const { user } = useContext(AuthContext);    const { loading, data } = useQuery(FETCH_POSTS_QUERY);    if (data) {        posts = data.getPosts;    }一切正常,但如果我添加一個(gè)新帖子更新阿波羅緩存,該緩存會(huì)正確更新舊帖子和新帖子,但前端沒有顯示它,只顯示舊帖子,直到我手動(dòng)刷新頁面。編輯:這是來自PostForm組件的代碼,我也更新了Home組件,添加了PostForm:function PostForm(props) {    const { values, handleChange, handleSubmit } = useForm(createPostCallback, {        title: 'Example Title',        body: ''    });    const [createPost] = useMutation(CREATE_POST_MUTATION, {        variables: values,        update(dataProxy, result) {            const data = dataProxy.readQuery({                query: FETCH_POSTS_QUERY            });            data.getPosts = [result.data.createPost, ...data.getPosts];            dataProxy.writeQuery({                query: FETCH_POSTS_QUERY,                data            });            values.body = '';        }    });知道如何解決第一個(gè)代碼問題嗎?提前謝謝各位朋友!
查看完整描述

3 回答

?
大話西游666

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊

apollo 中對(duì)讀寫緩存的查詢以不可變的方式工作。為了做到這一點(diǎn),您必須使用一個(gè)新變量,您正在使用數(shù)據(jù)來寫入緩存。嘗試這樣做:


  const [createPost] = useMutation(CREATE_POST_MUTATION, {

    variables: values,

    update (proxy, result) {

      const data = proxy.readQuery({

        query: FETCH_POSTS_QUERY

      })

      const new_post = result.data.createPost //here's the new var

      proxy.writeQuery({

        query: FETCH_POSTS_QUERY,

        data: { getPosts: [new_post, ...data.getPosts] } // here you're using that var to write the cache

      })

      values.body = ''

    }

  })


查看完整回答
反對(duì) 回復(fù) 2021-11-18
?
溫溫醬

TA貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊

我通過將數(shù)據(jù)定義為對(duì)象 {} 修復(fù)了相同的錯(cuò)誤,

只是通過添加更改了以下代碼= {}


const {

  loading,

  data: { getPosts: posts } = {}

} = useQuery(FETCH_POSTS_QUERY);


查看完整回答
反對(duì) 回復(fù) 2021-11-18
?
交互式愛情

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊

我會(huì)接受您的if聲明并將其設(shè)置在 a 中,useEffect以便它檢查數(shù)據(jù)是否是真實(shí)的 onLoad,這樣您就可以在數(shù)據(jù)更改時(shí)將其同步。


const [posts, setPosts] = useState([]);

useEffect(() => {

  if (data) {

    setPosts(data.getPosts);

  }

},[data])


if (posts.length === 0) {

  return <h3>No posts as of yet</h3>

}


查看完整回答
反對(duì) 回復(fù) 2021-11-18
  • 3 回答
  • 0 關(guān)注
  • 251 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)