Rendering a function with React – kentcdodds

  • But there are reasons for the API as it is and that’s not what we’re going over in this newsletter…With ReactSo thinking about this in the context of React:const getHomeContent = getContent(‘pages.home’)const ui = ( a href=”/about” {getHomeContent(‘nav.about’)} /a)// that’ll get you:a href=”/about”About/aSo far so good.
  • Anyway, this will break the app:const getHomeContent = getContent(‘pages.typo’)const ui = ( a href=”/about” {getHomeContent(‘nav.about’)} /a)// 💥 error 💥Again, this is happening because getContent(‘pages.typo’) will return the string {pages.typo} (to indicate that there’s no content at that path and the developer needs to fix that problem to get the content)….
  • The issue is that you can’t invoke a string but that’s what’s happening because getHomeContent is a string, not a function.A solution and a new problemSo the change I made this week makes it so when there’s no content at a given path, instead of a string, it returns a “sorta-curried”…
  • No problem.So now this wont throw an error, but we lose rendering the path if there’s no content!const getHomeContent = getContent(‘pages.typo’)const ui = ( a href=”/about” {getHomeContent(‘nav.about’)} /a)// that’ll get you:a href=”/about”/aAnd we want to make sure that we show the missing content so it’s more obvious for developers (yes…
  • Let’s rewrite the above to make this more clear:const getHomeContent = getContent(‘pages.typo’)const aboutContent = ui = a in this example is a function because the call to getContent had a typo, so we’ll never actually find content that matches the full path.

NOTE: This is a cross-post from my newsletter. I publish each email two weeks after it’s sent. Subscribe to get more content like this earlier right in your inbox! 💌 This week I was working on an…
Continue reading “Rendering a function with React – kentcdodds”

Array Methods Explained : Filter vs Map vs Reduce vs Foreach

  • we all know why this method is used for and even you don’t know about this method the name pretty much explains everything.Foreach takes a callback function and run that callback function on each element of array one by one.var sample = [1, 2, 3];// es5sample.forEach(function (elem, index){ console.log(elem + ‘ comes at ‘ + index);})// es6sample.forEach((elem, index) = `${elem} comes at ${index}`)/*output1 comes at 02 comes at 13 comes at 2*/For every element on the array we are calling a callback which gets element its index provided by foreach.Basically forEach works as a traditional for loop looping over the array and providing you array elements to do operations on them.okay!
  • If the value is true element remains in the resulting array but if the return value is false the element will be removed for the resulting array.var sample = [1, 2, 3] // yeah same array// es5var result = sample.filter(function(elem){ return elem !
  • As a ReactJS developer I use map a lot inside my application UI.Map like filter foreach takes a callback and run it against every element on the array but whats makes it unique is it generate a new array based on your existing array.Let’s understand map with an examplevar sample = [1, 2, 3] // i am never gonna change Boo!
  • Yeah// es5var mapped = sample.map(function(elem) { return elem * 10;})// es6let mapped = sample.map(elem = elem * 10)console.log(mapped);/* output */[10, 20, 30]Map ran through every element of the array, multiplied it to 10 and returned the element which will be going to store inside our resulting array.Like filter, map also returns an array.
  • ReduceAs the name already suggest reduce method of the array object is used to reduce the array to one single value.For example if you have to add all the elements of an array you can do something like this.var sample = [1, 2, 3] // here we meet again// es5var sum = sample.reduce(function(sum, elem){ return sum + elem;})// es6var sum = sample.reduce((sum, elem) = sum + elem)console.log(sum)reduce takes a callback ( like every function we talked about ).

Okay so yeah we know they are different they have different purpose and goals still we don’t bother to understand them. We use arrays. A lot of arrays. We use arrays to show search lists, items added…
Continue reading “Array Methods Explained : Filter vs Map vs Reduce vs Foreach”

💥 Just released some optimizations to react-localize-redux. mmmmjoy 💥 #reactjs #Redux

💥 Just released some optimizations to react-localize-redux. mmmmjoy 💥 #reactjs #Redux

  • Typically you will store your translation data in json files, but the data can also be a vanilla JS object.
  • Once your translation data is in the correct format use the addTranslation action creator.
  • A selector that takes your redux and returns the raw translation data.
  • Redux action creator to set which languages you are supporting in your translations.
  • Redux action creator to add new translation data to your redux store.

Dead simple localization for your React/Redux components
Continue reading “💥 Just released some optimizations to react-localize-redux. mmmmjoy 💥 #reactjs #Redux”

Using JavaScript’s map function – David Ford – Medium

Using JavaScript’s map function  #react #javascript #reactjs

  • Using JavaScript’s map functionThe map function simply transforms one array into an other array.
  • For example, suppose you have an array of person objects but what you really need is an array of names (strings):The map function will convert the array of person objects into an array of names (strings).
  • Lets say this is the array you start with:const friends = [ {id:1, name: ‘Dave’,age:50}, {id:2,name: ‘Kellie’,age:42}, {id:3,name: ‘Max’,age:12}, {id:2,name: ‘Jack’,age:12}];and you want to convert it to this:[‘Dave’, ‘Kellie’, ‘Max’,’Jack’]You start by defining a mapping function.
  • The mapping function operates on a single row, converting (or mapping) one person into one name (string):const mappingFunction = p – p.name;Then you pass the mapping function to array.map like this:const names = returns:[‘Dave’, ‘Kellie’, ‘Max’,’Jack’]You can also do this as a one liner:const names = friends.map(p – p.name);Map function in React JSXThis type of thing is very common in React JSX.
  • To map an array of person objects to an array of li elements, use a mapping function like this:const mappingFunction = p – li{p.name}/li;To ensure React’s DOM diff’ing works correctly, you’ll need to provide each li with a unique key:const mappingFunction = p – li key={p.id}{p.name}/li;In context, this might look something like this:render(){ return ul {friends.map(p – li key={p.id}{p.name}/li)} /ul;}

The map function simply transforms one array into an other array. For example, suppose you have an array of person objects but what you really need is an array of names (strings): You start by…
Continue reading “Using JavaScript’s map function – David Ford – Medium”

Just for Node.js & React.js Developers: A New Neo4j Movies Template – Medium

The Front-end: The front-end, in this case is built in React.js, Continue readi...  #react

  • Never miss a story from ReactJS News , when you sign up for Medium.
  • #ReactJS and React Native Stories for JavaScript Web Architects, Builders, Ninjas and the like.
  • If you run into problems, make sure your database is actually running and that you’ve entered your database credentials in the api/config.js file.
  • In the app, calls to the database are handled by /api/neo4j/dbUtils.js , described piece by piece below.
  • The Front-end: The front-end, in this case is built in React.js, consumes the data presented by the Express.js API and presents some views to the user, including the home page, a Movie detail page and a Person detail page.

“Just for Node.js & React.js Developers: A New Neo4j Movies Template” is published by ReactJS News
Continue reading “Just for Node.js & React.js Developers: A New Neo4j Movies Template – Medium”

Accessing Frontend Data with React and Redux

Accessing frontend data with #ReactJS and #Redux:

  • Redux.js only has one store with a single state object that holds all the front end data needed for your app.
  • The data I needed for this view was the array of dog walkers.
  • In order to display that data, I used a component state property called animals, which would represent the animal objects in an array.
  • The state object stored in the store is like any other JavaScript object.
  • Essentially any component that is connected to the state object can access any key-value pair it needs, making working with the single Redux store very convenient.

Over the last few weeks, I have begun to compare the flux flow of React.js and Redux.js’s flow while working on projects. The two are both very powerful and work very well with front end views that users interact with. I learned React first, working in the flux flow, and recently have been developing with React-Redux.…
Continue reading “Accessing Frontend Data with React and Redux”

Building a React.js App: Component Validation with PropTypes

Building a React.js App: Component Validation with PropTypes by @tylermcginnis33  #React

  • So looking at how the repos component is being used, you’ll notice we’re passing in username and we’re passing in a repo .
  • Now you’ll notice we get this warning, “Failed propType required username was not specified in repos .”
  • We don’t have to have it required, but as we talked about earlier when we use repos, we basically need a username and a repos array for this, so let’s go ahead and now when we render our repos component, let’s go ahead and take out username and let’s see what happens.
  • If we come in and we just render the notes component list this,
  • So everything is working correctly, but now what we’ve done is we’ve made the userProfile , the repos , and the notes component all validate that they’re being used correctly .

In this video, we’ll add PropTypes to some of our existing components to ensure that each component gets the data it needs to function properly.
Continue reading “Building a React.js App: Component Validation with PropTypes”