React’s ⚛️ new Context API – DailyJS – Medium

#ReactJS’s ⚛️ new Context #API:  by @kentcdodds #JavaScript

  • Here’s the simplest useful example I can come up with: – Here’s an even simpler version so you don’t have to open the codesandbox: – const ThemeContext = ThemeProvider extends React.Component { – state = {theme: ‘light’} – render() { – return ( – ThemeContext.Provider value={this.state.theme} – {this.props.children} – /ThemeContext.Provider…
  • , but if that’s not your jam, you could easily implement a Higher Order Component or something else using the context API (which is why it’s the best).
  • The React team will remove the warning about context being an experimental feature because it’s now a “first-class featureâ€� of the framework.
  • Here’s an example: – const ThemeContext = ThemeProvider extends React.Component {/* code */} – const ThemeConsumer = ThemeContext.Consumer – const LanguageContext = LanguageProvider extends React.Component {/* code */} – const LanguageConsumer = AppProviders({children}) { – return ( – LanguageProvider – ThemeProvider – {children} – /ThemeProvider – /LanguageProvider – ) -…
  • react-fns: Browser API’s turned into declarative React components and HoC’s by Jared Palmer ğŸ‘�react-composer: Compose render prop components (what I use in the codesandbox above) by jmeasreact-contextual: Tiny helper around Reacts new context API by Paul HenschelSome tweets from this last week: – P.S.

It’s way more ergonomic, it’s no longer “experimental,” and it’s now a first-class API! OH, AND IT USES A RENDER PROP! NOTE: This is a cross-post from my newsletter. I publish each email to my blog…
Continue reading “React’s ⚛️ new Context API – DailyJS – Medium”

React’s ⚛️ new Context API

  • The provider component puts the data into context, and the Higher Order Component pulls the data out of context.
  • The first pull request to that repository is from Andrew Clark (react core team member) and it’s called “New version of context”.
  • , but if that’s not your jam, you could easily implement a Higher Order Component or something else using the context API (which is why it’s the best).
  • The React team will remove the warning about context being an experimental feature because it’s now a “first-class feature” of the framework.
  • One question that I’ve seen a lot about the new context API (or the render prop pattern in general) is how to compose providers and consumers together.

React’s new context API way more ergonomic, it’s no longer “experimental,” and it’s now a first-class API! OH, AND IT USES A RENDER PROP!
Continue reading “React’s ⚛️ new Context API”

React’s ⚛️ new Context API – DailyJS – Medium

React’s ⚛️ new Context API   #reactjs

  • Here’s the simplest useful example I can come up with: – Here’s an even simpler version so you don’t have to open the codesandbox: – const ThemeContext = ThemeProvider extends React.Component { – state = {theme: ‘light’} – render() { – return ( – ThemeContext.Provider value={this.state.theme} – {this.props.children} – /ThemeContext.Provider…
  • , but if that’s not your jam, you could easily implement a Higher Order Component or something else using the context API (which is why it’s the best).
  • The React team will remove the warning about context being an experimental feature because it’s now a “first-class featureâ€� of the framework.
  • Here’s an example: – const ThemeContext = ThemeProvider extends React.Component {/* code */} – const ThemeConsumer = ThemeContext.Consumer – const LanguageContext = LanguageProvider extends React.Component {/* code */} – const LanguageConsumer = AppProviders({children}) { – return ( – LanguageProvider – ThemeProvider – {children} – /ThemeProvider – /LanguageProvider – ) -…
  • react-fns: Browser API’s turned into declarative React components and HoC’s by Jared Palmer ğŸ‘�react-composer: Compose render prop components (what I use in the codesandbox above) by jmeasreact-contextual: Tiny helper around Reacts new context API by Paul HenschelSome tweets from this last week: – P.S.

It’s way more ergonomic, it’s no longer “experimental,” and it’s now a first-class API! OH, AND IT USES A RENDER PROP! NOTE: This is a cross-post from my newsletter. I publish each email to my blog…
Continue reading “React’s ⚛️ new Context API – DailyJS – Medium”

React’s ⚛️ new Context API

  • The provider component puts the data into context, and the Higher Order Component pulls the data out of context.
  • The first pull request to that repository is from Andrew Clark (react core team member) and it’s called “New version of context”.
  • , but if that’s not your jam, you could easily implement a Higher Order Component or something else using the context API (which is why it’s the best).
  • The React team will remove the warning about context being an experimental feature because it’s now a “first-class feature” of the framework.
  • One question that I’ve seen a lot about the new context API (or the render prop pattern in general) is how to compose providers and consumers together.

React’s new context API way more ergonomic, it’s no longer “experimental,” and it’s now a first-class API! OH, AND IT USES A RENDER PROP!
Continue reading “React’s ⚛️ new Context API”

React’s ⚛️ new Context API – DailyJS – Medium

  • Here’s the simplest useful example I can come up with:Here’s an even simpler version so you don’t have to open the codesandbox:const ThemeContext = ThemeProvider extends React.Component { state = {theme: ‘light’} render() { return ( ThemeContext.Provider value={this.state.theme} {this.props.children} /ThemeContext.Provider ) }}class App extends React.Component { render() { ThemeProvider ThemeContext.Consumer…
  • The React team will remove the warning about context being an experimental feature because it’s now a “first-class feature” of the framework.
  • But even when we do feel it, we’ll have a solid, core React API to lean on to help us avoid the problem.Practical ContextOne question that I’ve seen a lot about the new context API (or the render prop pattern in general) is how to compose providers and consumers together….
  • Here’s an example:const ThemeContext = ThemeProvider extends React.Component {/* code */}const ThemeConsumer = ThemeContext.Consumerconst LanguageContext = React.createContext(‘en’)class LanguageProvider extends React.Component {/* code */}const LanguageConsumer = AppProviders({children}) { return ( LanguageProvider ThemeProvider {children} /ThemeProvider /LanguageProvider )}function { return ( LanguageConsumer {language = ( ThemeConsumer {theme = children({language, theme})} /ThemeConsumer )} /LanguageConsumer…
  • react-fns: Browser API’s turned into declarative React components and HoC’s by Jared Palmer 👏react-composer: Compose render prop components (what I use in the codesandbox above) by jmeasreact-contextual: Tiny helper around Reacts new context API by Paul HenschelSome tweets from this last week:P.

It’s way more ergonomic, it’s no longer “experimental,” and it’s now a first-class API! OH, AND IT USES A RENDER PROP! NOTE: This is a cross-post from my newsletter. I publish each email to my blog…
Continue reading “React’s ⚛️ new Context API – DailyJS – Medium”

The Beginner’s Guide to ReactJS – kentcdodds

  • You can find it here: http://kcd.im/news-a11yTwo weeks ago, my newsletter was an overview of the Advanced React Component Patterns course (that’s been published today!)
  • This week I’d like to talk a little bit about the other course that was published: The Beginner’s Guide to ReactJS.In the beginner’s course every lesson is an individual index.html file.
  • If you ever teach a total beginner React, make sure that you help them get a solid understanding that creating React elements is not magic and that JSX is a really simple abstraction on top of React.createElement.
  • Learn React Fundamentals and Advanced Patterns — I’m just pretty excited about the course release!Egghead.io Sale — Save 30% on a year subscription!ReactPrimer – React component prototyping tool that generates fully connected class component code.Partial Application Lambda Parameter Syntax for JavaScript — Awesome use of babel-macrosUsing Glitch as a UNIX command line playground — Interesting idea!Some tweets from…
  • 🧟🧟‍♂️🏹 29 Nov 2017I’m having a conversation about semicolons again️ 30 Nov 2017Next language I learn will be @reasonml 👌 3 Dec my big announcement!My @eggheadio courses have been published and are totally FREE!“Learn React Fundamentals and Advanced Patterns” 4 Dec 2017P.

The course is free forever on Egghead.io! Let’s talk about how it teaches even developers experienced with React ⚛️ NOTE: This is a cross-post from my newsletter. I publish each email two weeks after…
Continue reading “The Beginner’s Guide to ReactJS – kentcdodds”

Advanced React Component Patterns – kentcdodds

Advanced #ReactJS Component Patterns:  by @kentcdodds #JavaScript

  • Two of my courses will be (have now been) released as part of Egghead’s annual Christmas course release celebration 🎉 One is The Beginner’s Guide to ReactJS and the other is Advanced React Component Patterns.
  • 👀The course starts with a single lesson where I implement a simple toggle component that manages its own state.
  • My hope is that by doing things this way you can quickly identify the trade-offs of the different patterns and know when each pattern would be most appropriate.The course wraps up by refactoring the toggle component to a redux component affectionately called “Rendux.” I think that you’ll be blown away…
  • This way we can easily create custom components which have access to the toggle component’s on state and the toggle helper function.There are actually six lessons dedicated to Higher Order Components in this course.
  • Hopefully with this refactor you’ll see both the power and the simplicity of the render prop pattern.The basic idea of the pattern is that rather than have the toggle component be responsible for doing anything special in the render method, we delegate that responsibility over to the user and we…

An overview of some of the patterns covered in my recently released Egghead.io course (free until Friday). NOTE: This is a cross-post from my newsletter. I publish each email two weeks after it’s…
Continue reading “Advanced React Component Patterns – kentcdodds”

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”

Improving the usability of your modules – kentcdodds

Improving the usability of your modules  #reactjs

  • All you have to do is use it:import getContentForFile from ‘react-i18n’const i18n = App() { return ( div h1{i18n(‘header.title’)}/h1 /div )}So that’s how it works (again, I’m sure some of you are thinking of other libs that could do this better, but please spare me the “well actually.” I’m aware…
  • const content = JSON.parse( … more stuffexport {getContentForFile as default, init}This presents a few challenges for users of the module.
  • Turns out that react-i18n actually exposed another module react-i18n/bootstrap to customize this behavior which is great, but that doesn’t resolve the problem of stuff happening if someone were to import react-i18n first.So what I did was a wrapped all side-effects in a function I exported called init(which was similar to…
  • But it’s ok now because that’s clear const messages = JSON.parse( ) // … other other stuff}// … more stuffexport {getContentForFile as default, init}So this means that anyone using the module now must call the init function, but they’re doing that on their own terms and whenever they want it…
  • However, we’re in the process of “inner sourcing” this module (and perhaps open sourcing it eventually), so folks are going to use it who use different tools and have different use cases.So, if it’s not too much work and doesn’t add too much complexity, then try to make the solution…

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 last week I worked on my…
Continue reading “Improving the usability of your modules – kentcdodds”