All Projects → donavon → React Af

donavon / React Af

Licence: mit
Allows you to code using certain React.next features today! Perfect for component library maintainers.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Af

Create React Context
Polyfill for the proposed React context API
Stars: ✭ 689 (+381.82%)
Mutual labels:  context, polyfill
mini-create-react-context
(A smaller) polyfill for the react context API
Stars: ✭ 34 (-76.22%)
Mutual labels:  polyfill, context
Polyfill Php55
This component provides functions unavailable in releases prior to PHP 5.5.
Stars: ✭ 105 (-26.57%)
Mutual labels:  polyfill
Ts Polyfill
Runtime polyfills for TypeScript libs, powered by core-js! 🔋 🔩
Stars: ✭ 122 (-14.69%)
Mutual labels:  polyfill
React Workshop
⚒ 🚧 This is a workshop for learning how to build React Applications
Stars: ✭ 114 (-20.28%)
Mutual labels:  context
Formcat
A simple and easy way to control forms in React using the React Context API
Stars: ✭ 106 (-25.87%)
Mutual labels:  context
Eqcss
EQCSS is a CSS Reprocessor that introduces Element Queries, Scoped CSS, a Parent selector, and responsive JavaScript to all browsers IE8 and up
Stars: ✭ 1,513 (+958.04%)
Mutual labels:  polyfill
React Suspense Polyfill
Polyfill for the React Suspense API 😮
Stars: ✭ 99 (-30.77%)
Mutual labels:  polyfill
History.js
History.js gracefully supports the HTML5 History/State APIs (pushState, replaceState, onPopState) in all browsers. Including continued support for data, titles, replaceState. Supports jQuery, MooTools and Prototype. For HTML5 browsers this means that you can modify the URL directly, without needing to use hashes anymore. For HTML4 browsers it wi…
Stars: ✭ 10,761 (+7425.17%)
Mutual labels:  polyfill
Pure
🚱 Is a lightweight HTTP router that sticks to the std "net/http" implementation
Stars: ✭ 113 (-20.98%)
Mutual labels:  context
Touch Action
Disable 300ms delay on mobile using CSS touch-action or asynchronously download FastClick as polyfill
Stars: ✭ 119 (-16.78%)
Mutual labels:  polyfill
Theraot
Backporting .NET and more: LINQ expressions in .net 2.0 - nuget Theraot.Core available.
Stars: ✭ 112 (-21.68%)
Mutual labels:  polyfill
Resize Observer Polyfill
A polyfill for the Resize Observer API
Stars: ✭ 1,530 (+969.93%)
Mutual labels:  polyfill
Pex Context
Modern WebGL state wrapper for PEX: allocate GPU resources (textures, buffers), setup state pipelines and passes, and combine them into commands.
Stars: ✭ 117 (-18.18%)
Mutual labels:  context
Nullable
A source code only package which allows you to use .NET's nullable attributes in older target frameworks like .NET Standard 2.0 or the "old" .NET Framework.
Stars: ✭ 106 (-25.87%)
Mutual labels:  polyfill
Document.scrollingelement
A polyfill for document.scrollingElement as defined in the CSSOM specification.
Stars: ✭ 122 (-14.69%)
Mutual labels:  polyfill
Polyfill Util
This component provides binary-safe string functions, using the mbstring extension when available.
Stars: ✭ 1,364 (+853.85%)
Mutual labels:  polyfill
Browser Shims
Browser and JS shims used by Airbnb.
Stars: ✭ 112 (-21.68%)
Mutual labels:  polyfill
Fixed Sticky
DEPRECATED: A position: sticky polyfill that works with filamentgroup/fixed-fixed for a safer position:fixed fallback.
Stars: ✭ 1,490 (+941.96%)
Mutual labels:  polyfill
Contextism
😍 Use React Context better.
Stars: ✭ 141 (-1.4%)
Mutual labels:  context

react-af

Build Status npm version

React AF graffiti wall

TL;DR

  • Allows you to code using certain React.next features today!
  • Perfect for component library maintainers.
  • It does for React what Babel does for JavaScript.
  • Support getDerivedStateFromProps on older versions of React.
  • Supports Fragment on older versions of React.
  • Supports createContext (the new context API) on older versions of React.

What is this project?

Starting with React 17, several class component lifecycles will be deprecated: componentWillMount, componentWillReceiveProps, and componentWillUpdate (see React RFC 6).

One problem that React component library developers face is that they don't control the version of React that they run on — this is controlled by the consuming application. This leaves library developers in a bit of a quandary. Should they use feature detection or code to the lowest denominator?

react-af emulates newer features of React on older versions, allowing developers to concentrate on the business problem and not the environment.

Install

Install react-af using npm:

$ npm install react-af --save

or with Yarn:

$ yarn add react-af

Import

In your code, all you need to do is change the React import from this:

import React from 'react';

To this:

import React from 'react-af';

That's it! You can now code your library components as though they are running on a modern React (not all features supported... yet), even though your code may be running on an older version.

react-af imports from react under the hood (it has a peerDependency of React >=15), patching or passing through features where necessary.

API

Here are the modern React features that you can use, even if yur code is running on older version of React 15 or React 16.

getDerivedStateFromProps

react-af supports new static lifecycle getDerivedStateFromProps.

Here is an example component written using componentWillReceiveProps.

class ExampleComponent extends React.Component {
  state = { text: this.props.text };

  componentWillReceiveProps(nextProps) {
    if (this.props.text !== nextProps.text) {
      this.setState({
        text: nextProps.text
      });
    }
  }
}

And here it is after converting to be compatible with modern React.

class ExampleComponent extends React.Component {
  state = {};

  static getDerivedStateFromProps(nextProps, prevState) {
    return prevState.text !== nextProps.text
      ? {
        text: nextProps.text
      }
      : null;
  }
}

Fragment

Starting with React 16.2, there is a new <Fragment /> component that allows you to return multiple children. Prior to 16.2, you needed to wrap multiple children in a wrapping div.

With react-af, you can use React.Fragment on older versions of React as well.

import React, { Fragment } from 'react-af';

const Weather = ({ city, degrees }) => (
  <Fragment>
    <div>{city}</div>
    <div>{degrees}</div>
  </Fragment>
);

The code above works natively in React 16.2 and greater. In lesser versions of React, Fragment is replaced with a div automatically.

createContext

React 16.3 also added support for the new context API. Well react-af supports that as well.

Here's an example take from Kent Dodds's article React’s new Context API.

import React, { createContext, Component } from 'react-af';

const ThemeContext = createContext('light')
class ThemeProvider extends Component {
  state = {theme: 'light'}
  render() {
    return (
      <ThemeContext.Provider value={this.state.theme}>
        {this.props.children}
      </ThemeContext.Provider>
    )
  }
}
class App extends Component {
  render() {
    return (
      <ThemeProvider>
        <ThemeContext.Consumer>
          {val => <div>{val}</div>}
        </ThemeContext.Consumer>
      </ThemeProvider>
    )
  }
}

Other projects

react-lifecycles-compat

You might also want to take a look at react-lifecycles-compat by the React team. It doesn't support Fragment or createContext and it requires additional plumbing to setup, but it's lighter and may be adequate for some projets.

create-react-context

If all you need is context support, consider using create-react-context, which is what this package uses to emulate createContext().

What's with the name?

ReactAF stands for React Always Fresh (or React As F&#%!). Your choice.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].