-
Cross post from reddit, but I don't seem to be getting much response there. I am working on understanding the
The list of articles would start out empty. Then when the user clicks the button, the list is populated. The project works, but I'm not sure I've written the functionality in the most efficient way and could use a double check. For reference, I used the official docs and Darren Ethier's series of articles on the subject. After registering a working block, I wrote this small react app (I've merged everything into one file here for simplicity). I have a working example on codesandox. I have the following questions that hopefully you all can help me understand better
Thanks for any help you can give me!
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
@adamziel, maybe you can help with this question. |
Beta Was this translation helpful? Give feedback.
-
@aberkow You don't need a custom store if your code is running on
I played with your code snippet and here's how the simplest version of these components could look like – note they don't rely on a custom store: import { useEntityRecords } from '@wordpress/core-data';
const ArticleList = () => {
const { records: selectedArticles } = useEntityRecords('postType', 'page');
return (
<>
<h1>Latest Articles</h1>
{
selectedArticles && (
<ul>
{
selectedArticles.map((article, index) => {
return <li key={index}>{article.title.rendered}</li>
})
}
</ul>
)
}
</>
)
}
const App = () => {
return (
<div>
<ArticleList state={DEFAULT_STATE} />
</div>
)
} There is no clickable button, though. Gutenberg will resolve the pages whenever the import { useState } from '@wordpress/element';
const App = () => {
const [showArticles, setShowArticles] = useState(false);
return (
<>
<div>
<button
onClick={() => setShowArticles(true) }
>GET Articles</button>
{showArticles && (
<ArticleList state={DEFAULT_STATE} />
)}
</div>
</>
)
} And if you'd like to use a custom store, here's how one would look like using Thunks: const store = createReduxStore('my/data-demo', {
// these are action creators. they return an action object with a type and arbitrary state data
// they help the app maintain an immutable state
// they are determined by the dispatch method calls
actions,
selectors: {
// the state is implied. it doesn't need to be passed when the selector is called
getArticles(state) {
return state.articles
}
},
resolvers: {
/**
*
* You can pass an arbitrary number of arguments here.
* The first will always be the state. After that, you can use anything
* The other arguments _do not_ need to be represented in the selector function. However, that selector must return state
*
* See here for info on hasFinishedResolution - https://unfoldingneurons.com/2020/wordpress-data-store-properties-resolvers
*
* @param {*} state
* @param {*} test
* @returns
*/
getArticles: (state, test) => async ({ dispatch }) => {
const path = 'https://today.uconn.edu/wp-rest/wp/v2/posts'
const articles = await fetch(action.path).then(res => res.json()).catch(err => console.log({ err }));
dispatch.setArticles(articles)
}
},
// the reducer function returns new immutable copies of the state
// there should be no mutations
// once the dispatch method is called, the reducer determines how to create a new state object
reducer: (state = DEFAULT_STATE, action) => {
switch (action.type) {
case 'SET_ARTICLES': {
return {
...state,
articles: action.payload
}
}
// if there is no matching action return the state
default:
return state
}
}
} Regarding your questions:
Once the store is registered, calling
No.
In Gutenberg, fetching data via an action isn't a very popular pattern. In most cases we use a selector+resolver pair and allow the resolution to be triggered automatically when the selector is called for the first time. I hope this helps! |
Beta Was this translation helpful? Give feedback.
@aberkow You don't need a custom store if your code is running on
today.uconn.edu
domain – you can lean on the built-in@wordpress/core-data
utilities. Here's a few resources you may find helpful:I played with your code snippet and here's how the simplest version of these components could look like – note they don't rely on a custom store: