У меня есть приложение React, которое получает объект с сайта WordPress с помощью fetch () поверх rest api (v2 / posts).
Объект содержит запись "acf", которая содержит ~ 30 вложенных записей: некоторые массивы, а некоторые являются объектами. Все сгенерировано с помощью плагина Advanced Custom Field.
Приложение обновляет эти данные (используя this.state) и сервер (выборка / публикация) по запросу пользователя. Все работает. Я не использую экспресс, сокращение и тому подобное.
My current problem is that I cannot access the internals of the "acf" entry in render() function, while I can access it within the fetch response. I would like to be able to acccess
cpt['acf']['entry']['subentry']using the entry's names. But I get "cannot access property of undefined..."
Пример:
// In constructor: // this.state = { // isLoading: false, title:"", terms:[], cpt: [] // } ... fetch(url) .then(response => { if (response.ok) return response.json() throw new Error('fetchTerm: Something went wrong ...') }) .then(cpt => { var terms = Object.entries(cpt['acf']) var title = Object.values(cpt['title']) this.setState({cpt, terms, title, isLoading: false}) .then( () => { console.log("title",this.state.cpt['title']) console.log("FAMILYLOG",this.state.cpt['acf']['familylog_array']) // shows ok }) return (true) // void - no one is looking... }) //... render(){ if (this.state.isLoading) return (Loading) // occurs only after fetch resturn and this.state variables are instanciated console.log("title", this.state.title) // shows OK console.log("FAIL", this.state.cpt['title']) // cannot access property of NULL/Undefined... console.log("FAMILYLOG",this.state.cpt['acf']['familylog_array']) // same problem }
Что мне не хватает?
Я хочу получить доступ cpt['acf']['entry']['subentry']
Должен ли я регенерировать cpt в массивы / объекты в состоянии Component?