Migrating to React’s New Context API – kentcdodds

📝 Article => Migrating to React’s New Context API by @kentcdodds  | #ReactJS

  • Because of this significant change, I’m making an update to my advanced component patterns course on Egghead.io to use the new API rather than the old one.
  • The Old Context APIHere’s the version of the implementation with the old context API: – const TOGGLE_CONTEXT = ‘__toggle__’ – function ToggleOn({children}, context) { – const {on} = context[TOGGLE_CONTEXT] – return on ?
  • null : = { – [TOGGLE_CONTEXT]: ToggleButton(props, context) { – const {on, toggle} = context[TOGGLE_CONTEXT] – return ( – Switch on={on} onClick={toggle} {…props} / – ) – } – ToggleButton.contextTypes = { – [TOGGLE_CONTEXT]: Toggle extends React.Component { – static On = ToggleOn – static Off = ToggleOff – static…
  • on}), – () = ) – getChildContext() { – return { – [TOGGLE_CONTEXT]: { – on: this.state.on, – toggle: this.toggle, – }, – } – } – render() { – return div{this.props.children}/div – } – }With the old API, you had to specify a string for what context your component…
  • Here’s my new version of this same exercise: – const ToggleContext = React.createContext({ – on: false, – toggle: () = {}, – })class Toggle extends React.Component { – static On = ({children}) = ( – ToggleContext.Consumer – {({on}) = (on ?

With the recent release of React 16.3.0 came an official context API. You can learn more about the why and how behind this API from my previous blog post: “React’s ⚛️ new Context API”. Because of…
Continue reading “Migrating to React’s New Context API – kentcdodds”

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”

ReactJS, React Native & GraphQL Newsletter: 84

#ReactJS, React Native & GraphQL Newsletter: 84

  • This week we get updated on React Async Rendering, learn Render Props, use GraphQL with AWS, check out the Easter sale and much more!
  • Update on Async Rendering Brian Vaughn tells us lessons learnt while the React team has been working to implement asynchronous rendering….

Last updated on April 3rd, 2018Welcome to issue 84. This week we get updated on React Async Rendering, learn Render Props, use GraphQL with AWS, check out the Easter sale and much more! Update on Async Rendering Brian Vaughn tells us lessons learnt while the React team has been working to implement asynchronous rendering….
Continue reading “ReactJS, React Native & GraphQL Newsletter: 84”

Evolving Patterns in React

  • A good way of bypassing this is to use props destructuring together with JSX spread , as you can see here: – – So now, you can change the props needed for and be sure that those props are not referenced in multiple components.
  • If you are using Redux, you will recognize that the function is a HOC — takes your component and adds a bunch of props to it.
  • Let’s implement a basic HOC that can add props to existing components.
  • Each HOC will introduce an additional React Component in your DOM/vDOM structure.
  • Render props can be used in situations where you need some reusable logic inside the component and you don’t want to wrap your component in a HOC.

https://cdn-images-1.medium.com/max/2000/1*rJr_bOm3mD5V8_C5JaPrsQ.jpeg
Let’s take a closer look at some of the patterns that are emerging in the React ecosystem. These patterns improve readability,…
Continue reading “Evolving Patterns in React”

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”

What I learned making my first open source React component

  • A folder for your code, a folder for your demo app (plus dev server and hot loading, yay\o/), a folder that you should use and I didn’t, some build stuff for shipping… – – Basically, it gives you everything you need so you can focus on your component and don’t…
  • You need a demo page so you can show off your component in action.
  • react-lazyload-fadein, for example, has a demo page that shows off different ways you can use it.
  • People will find out about your component in different ways.
  • Especially if someone could figure out how to slurp in the README file and make that demo page for you 🤔 – – The React community is a bit silly and loves to invent new patterns to bicker about.

Continue reading “What I learned making my first open source React component”

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”

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”