Friday, June 30, 2017

node-webkit aka nw.js

Steps to run a sample node-webkit app on MacOS:


  • Downloaded the node-webkit from https://nwjs.io/downloads/. Extracted the zip content to ~/workspace (~/workspace/nwjs-sdk-v0.23.5-osx-x64).
  • Created a folder ~/workspace/my-nwjs-app and created package.json and index.html in that folder.
  • content of the package.json: {"name":"my-nwjs-app", "main":"index.html"}
  • content of the index.html: <bold>Hello</bold>
  • From my-nwjs-app, run the command:~/workspace/nwjs-sdk-v0.23.5-osx-x64/nwjs.app/Contents/MacOS/nwjs .

Sunday, June 11, 2017

Creating React Components

You can create stateless or stateful React components.

Let's start with the simple things first: the stateless components.

function MyStateLessComponent(props) {
         return <div />;  //See no quotes around the returning content
}

The same above in ES6:

MyStateLessComponent = (props)  => <div />

Now, to stateful components.

class MyStateFulComponent extends React.Component {
         constructor(props) {
                super(props)
         }
         render() {
               return <div />
         }
}

Another way of creating the stateful component in React:

var MyStateFulComponent = React.createClass({
         constuctor: function(props){
             super(props)
         }, //See the comma here ---don't forget this while doing the createClass
         render: function(){
             return <div />
         }
})

https://facebook.github.io/react/docs/components-and-props.html

https://facebook.github.io/react/docs/components-and-props.html





Thursday, June 8, 2017

Hey, you want to create-react-app?

Instead of installing tons of node modules and webpack, etc., etc., follow this github repo to easily create-react-app: https://github.com/facebookincubator/create-react-app. You are all set in 5-10 minutes.

Happy programming!

Wednesday, June 7, 2017

Array Map, Filter, Reduce methods (Array mfr in short)

Use map when you want to change each value in the array based on some logic

var mappedArray = [2,4].map(number => number * 2)

console.log(mappedArray)


Use filter when you want filter out some values from an array based on some logic

var filteredArray = [1,2,3,4].filter(number => (number % 2 === 0))

console.log(filteredArray)


Use reduce when you want to reduce an array into a single resulting value

var reducedArray = [1,2,3,4].reduce((total,number) => total + number, 0)

console.log(reducedArray)

Cute reference: https://medium.com/@joshpitzalis/the-trouble-with-loops-f639e3cc52d9