JavaScript ES 2017: Learn Async/Await by Example – codeburst

  • Consider the below code:function doubleAfter2Seconds(x) { return new Promise(resolve = { setTimeout(() = { resolve(x * 2); }, 2000); });}In this code we have a function called doubleAfter2Seconds.
  • Here’s what the boilerplate code looks like:function addPromise(x){ return new Promise(resolve = { // Code goes here… // resolve() });}Awesome.
  • In this example we should be returning x + 2*a + 2*b + 2*c. Here’s the code:function addPromise(x){ return new Promise(resolve = { = { = { = { resolve(x + a + b + c); }) }) }) });}Lets walk through the code again, line by line.First, we create…
  • Here’s what that looks like:async function addAsync(x) { // code here…}Now that you’ve created an async function, we can make use of the await keyword which will pause our code until the Promise has resolved.
  • Here’s how easy that is:async function addAsync(x) { const a = await doubleAfter2Seconds(10); const b = await doubleAfter2Seconds(20); const c = await doubleAfter2Seconds(30); return x + a + b + c;}And here’s the full code:As you can see, we’re still making use of the same doubleAfter2Seconds function.

ES 2017 introduced Asynchronous functions. Async functions are essentially a cleaner way to work with asynchronous code in JavaScript. In order to understand exactly what these are, and how they work…
Continue reading “JavaScript ES 2017: Learn Async/Await by Example – codeburst”

Introduce Redux local state to your React App – t-benze – Medium

Introduce Redux local state to your React App  #redux #react #reactjs

  • In this way I can extract the data fetching logic into actions and reducers, and take them out from the component to make sure the component is only dealing with UI stuff, and let actions and reducers handle the business logic.So narrow down the problem, what I want is the capability to create local state that is bound to an component in the redux store, which only lives within the component’s lifecycle, and still enjoy the benefits redux has provided (actions, reducers, middleware).
  • Our requirements are as follows:the component using local state should be able to access the global redux state naturallythe api to connect to local state should be similar as the connect function from react-reduxthe actions dispatched for local state need to work normally with redux middlewarethe actions and reducers need to be agnostic to whether they’re used with respect to local state or global stateImplementationThe local state in redux can be implemented as an add-on to redux quite straightforwardly.
  • To fix this, we create another middleware to apply after the redux thunk middleware.Since the actions dispatched by the local mapDispatchToProps will be scoped like the one below, redux thunk will now handle it and then our middleware will get to handle it, and if we see the localAction is a function, then we call the function as what thunk would do, but pass in the a localDispatch function and a localGetState function as the first two parameters, followed by dispatch and getState function for global state.
  • { type: ‘$LOCAL_DISPATCH_ACTION’, localAction: (localDispatch, localGetState) = {…}, id}ExampleBelow shows a simple app that has one counter reducer in the global state, along with a local reducer, which is empty at the beginning.Once we click show local to create a component that has its local state and reuse the counter reducer, an new state instance with unique id is registered in the local reducer.Within the component, we can dispatch both global actions and local actions, and they won’t affect each others.Once we remove the component, its local state will get discarded the state management in our app is a mixture of different approaches.
  • We have some UI components using setState() to mange their internal UI state that doesn’t concern others, we have some global application state like user info stored in the global redux store, and we also have some page containers which store its feature data in its local redux state.

I’ve been using redux in my recent projects, including both react web and react-native, and I really like its way of managing application state: one single data source, using actions and reducers to…
Continue reading “Introduce Redux local state to your React App – t-benze – Medium”

How to use React Bootstrap Counter – Medium

  • The input value will not gets updated if user try to enter value greater than max value.
  • This piece of code output the updated value every time the input value gets updated.
  • Whenever the value gets updated either by clicking Increment , Decrement or by directly entering the value onChange function will get fired with the newly updated value.
  • Solving people problems using code spells.
  • Like Max, The Min prop do the exact opposite thing a user cannot enter a value less than the Min value.

I built react-bootstrap-counter for facilitating the use of counter input in ReactJs projects , let’s take a look how you can too use it in…
Continue reading “How to use React Bootstrap Counter – Medium”