You can pass on an array (Monster) from data fetching to setting the state on monsters and filteredMonsters. You’ll notice filteredMonsters inherits the array from monsters and doesn’t need to be defined again.
Redux-Saga is a middleware library used to allow a Redux store to asynchronously interact with resources outside of itself. Redux-Saga helps in: making HTTP requests. accessing browser storage.
First install Redux Saga:
npm install redux-saga
Then update your store to include thunk to your middleware like so:
Sagas are implemented as Generator functions that yield objects to the redux-saga middleware. The yielded objects are a kind of instruction to be interpreted by the middleware. When a Promise is yielded to the middleware, the middleware will suspend the Saga until the Promise completes.
Here is an example of how a generator function works:
Create a file called category.saga.js and add your actions and asynchronous function:
Thunk allows us to dispatch actions manually, which gives us the power to incorporate some logic or run some asynchronous code before dispatching an action.
First install Redux Thunk:
npm install redux-thunk
Then update your store to include thunk to your middleware like so:
If you have any logic or asynchronous code you are running in your component that you run before you dispatch , now you can add those to your action files instead. First you’ll need to modify your reducer with new action types to handle these states.
Earlier we only had one action type which was GET_CATEGORIES. We now can have three different actions: START, SUCCESS and FAILED. This is because we will now have asynchronous code inside our actions file which will allow us to know the order of our actions.
The functions fetchCategoriesStart, fetchCategoriesSuccess, fetchCategoriesFailed are like earlier actions without thunk installed. It was also why we could only have one action. The new function that has the logic and asynchronous code is called fetchCategoriesAsync (Thunk recommends to include the word Async in the name so we know that this function is in fact asynchronous). This new function includes all our actions (start, success and failed) depending on what’s pulled back from the data source.
Now you only need to dispatch fetchCategoriesAsync inside our component shop:
Redux Devtools is a Chrome extension which you need to install first.
Then update your composer in your store to use Redux Devtools when not in the production environment and only when a window object exists (so it doesn’t break when building).
Now when you view your redux site in Chrome the Devtools extension will be highlighted and you can view all the live states and even do a playback of all the states. Very useful for debugging complex applications.
Currying a function is essentially a function that returns back another function. The reason you do this is maybe function “curryFunc” receives some variable “a”. What happens is the return function might use “a” inside of it. But what it does is it depends on two additional properties that you add to it as new parameters for this function.
constcurryFunc=(a)=>(b, c)=>{
a + b - c
}const with3 =curryFunc(3);const with10 =curryFunc(3);with3(2,4);// 3 + 2 - 4with10(9,2);// 3 + 9 - 2
The above example shows how you can create functions from a function (reusable functions).
This type of logic is very helpful when creating our own custom middleware functions for Redux.
Create a new view under new components folder called master.blade.php. This HTML template taken from Bootstrap:
<!doctypehtml><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1, shrink-to-fit=no"><metaname="description"content=""><metaname="author"content=""><linkrel="icon"href="https://getbootstrap.com/docs/4.0/assets/img/favicons/favicon.ico"><title>Blog Template for Bootstrap</title><linkrel="canonical"href="https://getbootstrap.com/docs/4.0/examples/blog/"><!-- Bootstrap core CSS --><linkhref="https://getbootstrap.com/docs/4.0/dist/css/bootstrap.min.css"rel="stylesheet"><!-- Custom styles for this template --><linkhref="https://fonts.googleapis.com/css?family=Playfair+Display:700,900"rel="stylesheet"><linkhref="{{asset('css/app.css')}}"rel="stylesheet"><linkhref="{{asset('css/blog.css')}}"rel="stylesheet"></head><body><divclass="container"><headerclass="blog-header py-3"><divclass="row flex-nowrap justify-content-between align-items-center"><divclass="col-4 pt-1"><aclass="text-muted"href="#">Subscribe</a></div><divclass="col-4 text-center"><aclass="blog-header-logo text-dark"href="#">Large</a></div><divclass="col-4 d-flex justify-content-end align-items-center"><aclass="text-muted"href="#"><svgxmlns="http://www.w3.org/2000/svg"width="20"height="20"viewBox="0 0 24 24"fill="none"stroke="currentColor"stroke-width="2"stroke-linecap="round"stroke-linejoin="round"class="mx-3"><circlecx="10.5"cy="10.5"r="7.5"></circle><linex1="21"y1="21"x2="15.8"y2="15.8"></line></svg></a><aclass="btn btn-sm btn-outline-secondary"href="#">Sign up</a></div></div></header><divclass="nav-scroller py-1 mb-2"><navclass="nav d-flex justify-content-between"><aclass="p-2 text-muted"href="#">World</a><aclass="p-2 text-muted"href="#">U.S.</a><aclass="p-2 text-muted"href="#">Technology</a><aclass="p-2 text-muted"href="#">Design</a><aclass="p-2 text-muted"href="#">Culture</a><aclass="p-2 text-muted"href="#">Business</a><aclass="p-2 text-muted"href="#">Politics</a><aclass="p-2 text-muted"href="#">Opinion</a><aclass="p-2 text-muted"href="#">Science</a><aclass="p-2 text-muted"href="#">Health</a><aclass="p-2 text-muted"href="#">Style</a><aclass="p-2 text-muted"href="#">Travel</a></nav></div><divclass="jumbotron p-3 p-md-5 text-white rounded bg-dark"><divclass="col-md-6 px-0"><h1class="display-4 font-italic">Title of a longer featured blog post</h1><pclass="lead my-3">Multiple lines of text that form the lede, informing new readers quickly and efficiently about what's most interesting in this post's contents.</p><pclass="lead mb-0"><ahref="#"class="text-white font-weight-bold">Continue reading...</a></p></div></div></div><mainrole="main"class="container"><divclass="row"><divclass="col-md-8 blog-main"><h3class="pb-3 mb-4 font-italic border-bottom">
From the Firehose
</h3><divclass="blog-post"><h2class="blog-post-title">Sample blog post</h2><pclass="blog-post-meta">January 1, 2014 by <ahref="#">Mark</a></p><p>This blog post shows a few different types of content that's supported and styled with Bootstrap. Basic typography, images, and code are all supported.</p><hr><p>Cum sociis natoque penatibus et magnis <ahref="#">dis parturient montes</a>, nascetur ridiculus mus. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Sed posuere consectetur est at lobortis. Cras mattis consectetur purus sit amet fermentum.</p><blockquote><p>Curabitur blandit tempus porttitor. <strong>Nullam quis risus eget urna mollis</strong> ornare vel eu leo. Nullam id dolor id nibh ultricies vehicula ut id elit.</p></blockquote><p>Etiam porta <em>sem malesuada magna</em> mollis euismod. Cras mattis consectetur purus sit amet fermentum. Aenean lacinia bibendum nulla sed consectetur.</p><h2>Heading</h2><p>Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p><h3>Sub-heading</h3><p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p><pre><code>Example code block</code></pre><p>Aenean lacinia bibendum nulla sed consectetur. Etiam porta sem malesuada magna mollis euismod. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa.</p><h3>Sub-heading</h3><p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean lacinia bibendum nulla sed consectetur. Etiam porta sem malesuada magna mollis euismod. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.</p><ul><li>Praesent commodo cursus magna, vel scelerisque nisl consectetur et.</li><li>Donec id elit non mi porta gravida at eget metus.</li><li>Nulla vitae elit libero, a pharetra augue.</li></ul><p>Donec ullamcorper nulla non metus auctor fringilla. Nulla vitae elit libero, a pharetra augue.</p><ol><li>Vestibulum id ligula porta felis euismod semper.</li><li>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</li><li>Maecenas sed diam eget risus varius blandit sit amet non magna.</li></ol><p>Cras mattis consectetur purus sit amet fermentum. Sed posuere consectetur est at lobortis.</p></div><!-- /.blog-post --><divclass="blog-post"><h2class="blog-post-title">Another blog post</h2><pclass="blog-post-meta">December 23, 2013 by <ahref="#">Jacob</a></p><p>Cum sociis natoque penatibus et magnis <ahref="#">dis parturient montes</a>, nascetur ridiculus mus. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Sed posuere consectetur est at lobortis. Cras mattis consectetur purus sit amet fermentum.</p><blockquote><p>Curabitur blandit tempus porttitor. <strong>Nullam quis risus eget urna mollis</strong> ornare vel eu leo. Nullam id dolor id nibh ultricies vehicula ut id elit.</p></blockquote><p>Etiam porta <em>sem malesuada magna</em> mollis euismod. Cras mattis consectetur purus sit amet fermentum. Aenean lacinia bibendum nulla sed consectetur.</p><p>Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p></div><!-- /.blog-post --><divclass="blog-post"><h2class="blog-post-title">New feature</h2><pclass="blog-post-meta">December 14, 2013 by <ahref="#">Chris</a></p><p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean lacinia bibendum nulla sed consectetur. Etiam porta sem malesuada magna mollis euismod. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.</p><ul><li>Praesent commodo cursus magna, vel scelerisque nisl consectetur et.</li><li>Donec id elit non mi porta gravida at eget metus.</li><li>Nulla vitae elit libero, a pharetra augue.</li></ul><p>Etiam porta <em>sem malesuada magna</em> mollis euismod. Cras mattis consectetur purus sit amet fermentum. Aenean lacinia bibendum nulla sed consectetur.</p><p>Donec ullamcorper nulla non metus auctor fringilla. Nulla vitae elit libero, a pharetra augue.</p></div><!-- /.blog-post --><navclass="blog-pagination"><aclass="btn btn-outline-primary"href="#">Older</a><aclass="btn btn-outline-secondary disabled"href="#">Newer</a></nav></div><!-- /.blog-main --><asideclass="col-md-4 blog-sidebar"><divclass="p-3 mb-3 bg-light rounded"><h4class="font-italic">About</h4><pclass="mb-0">Etiam porta <em>sem malesuada magna</em> mollis euismod. Cras mattis consectetur purus sit amet fermentum. Aenean lacinia bibendum nulla sed consectetur.</p></div><divclass="p-3"><h4class="font-italic">Archives</h4><olclass="list-unstyled mb-0"><li><ahref="#">March 2014</a></li><li><ahref="#">February 2014</a></li><li><ahref="#">January 2014</a></li><li><ahref="#">December 2013</a></li><li><ahref="#">November 2013</a></li><li><ahref="#">October 2013</a></li><li><ahref="#">September 2013</a></li><li><ahref="#">August 2013</a></li><li><ahref="#">July 2013</a></li><li><ahref="#">June 2013</a></li><li><ahref="#">May 2013</a></li><li><ahref="#">April 2013</a></li></ol></div><divclass="p-3"><h4class="font-italic">Elsewhere</h4><olclass="list-unstyled"><li><ahref="#">GitHub</a></li><li><ahref="#">Twitter</a></li><li><ahref="#">Facebook</a></li></ol></div></aside><!-- /.blog-sidebar --></div><!-- /.row --></main><!-- /.container --><footerclass="blog-footer"><p>Blog template built for <ahref="https://getbootstrap.com/">Bootstrap</a> by <ahref="https://twitter.com/mdo">@mdo</a>.</p><p><ahref="#">Back to top</a></p></footer><!-- Bootstrap core JavaScript
================================================== --><!-- Placed at the end of the document so the pages load faster --><scriptsrc="https://code.jquery.com/jquery-3.2.1.slim.min.js"integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"crossorigin="anonymous"></script><script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery-slim.min.js"><\/script>')</script><scriptsrc="../../assets/js/vendor/popper.min.js"></script><scriptsrc="../../dist/js/bootstrap.min.js"></script><scriptsrc="../../assets/js/vendor/holder.min.js"></script><script>
Holder.addTheme('thumb',{bg:'#55595c',fg:'#eceeef',text:'Thumbnail'});</script></body></html>
In the welcome view replace with a component like this:
<x-master></x-master>
Part 2
Cut everything inside blog-main class of master.blade.php and add inside these tags of welcome.blade.php
<x-master>
@section('main-content')
Code here
@endsection
</x-master>
You can further make the main content contain another component by creating a new component called <x-test></x-test> inside the new section. Then in components folder create a template called test.blade.php and add the content you want.
Part 3
Pass data to components
Open routes to add proper controller to the route: