Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
react-codemod
Advanced tools
This repository contains a collection of codemod scripts based on JSCodeshift that help update React APIs.
npm install -g react-codemod
react-codemod <codemod-script> <file>
-d
option for a dry-run and use -p
to print the output
for comparisonfindDOMNode
updates this.getDOMNode()
or this.refs.foo.getDOMNode()
calls inside of React.createClass
components to React.findDOMNode(foo)
. Note
that it will only look at code inside of React.createClass
calls and only
update calls on the component instance or its refs. You can use this script to
update most calls to getDOMNode
and then manually go through the remaining
calls.
react-codemod findDOMNode <file>
pure-render-mixin
removes PureRenderMixin
and inlines
shouldComponentUpdate
so that the ES6 class transform can pick up the React
component and turn it into an ES6 class. NOTE: This currently only works if you
are using the master version (>0.13.1) of React as it is using
React.addons.shallowCompare
react-codemod pure-render-mixin <file>
--mixin-name=<name>
is specified it will look for the specified name
instead of PureRenderMixin
. Note that it is not possible to use a
namespaced name for the mixin. mixins: [React.addons.PureRenderMixin]
will
not currently work.class
transforms React.createClass
calls into ES6 classes.
react-codemod class <file>
--no-super-class=true
is specified it will not extend
React.Component
if setState
and forceUpdate
aren't being called in a
class. We do recommend always extending from React.Component
, especially
if you are using or planning to use Flow. Also make
sure you are not calling setState
anywhere outside of your component.All scripts take an option --no-explicit-require=true
if you don't have a
require('React')
statement in your code files and if you access React as a
global.
isMounted
, getDOMNode
,
replaceProps
, replaceState
or setProps
it will skip the component.var A = React.createClass(spec)
with
class A (extends React.Component) {spec}
.statics
plus the few special cased
statics like propTypes
, childContextTypes
, contextTypes
and
displayName
and assigns them after the class is created.
class A {}; A.foo = bar;
getDefaultProps
and inlines it as a static defaultProps
.
If getDefaultProps
is defined as a function with a single statement that
returns an object, it optimizes and transforms
getDefaultProps() { return {foo: 'bar'}; }
into
A.defaultProps = {foo: 'bar'};
. If getDefaultProps
contains more than
one statement it will transform into a self-invoking function like this:
A.defaultProps = function() {…}();
. Note that this means that the function
will be executed only a single time per app-lifetime. In practice this
hasn't caused any issues – getDefaultProps
should not contain any
side-effects.this.foo
but also traces variable
assignments like var self = this; self.foo
. It does not bind functions
from the React API and ignores functions that are being called directly
(unless it is both called directly and passed around to somewhere else)getInitialState
exists in the React.createClass
spec OR if functions
need to be bound to the instance.--no-super-class=true
is passed it only optionally extends
React.Component
when setState
or forceUpdate
are used within the
class.The constructor logic is as follows:
super(props, context)
if the base class needs to be extended.this.foo = this.foo.bind(this)
getInitialState
(and remove getInitialState
from the spec). It
also updates access of this.props.foo
to props.foo
and adds props
as
argument to the constructor. This is necessary in the case when the base
class does not need to be extended where this.props
will only be set by
React after the constructor has been run.return StateObject
from getInitialState
to assign this.state
directly.Options to recast's printer can be provided
through the printOptions
command line argument
react-codemod class <file> --printOptions='{"quote":"double"}'
FAQs
React codemod scripts
The npm package react-codemod receives a total of 12,125 weekly downloads. As such, react-codemod popularity was classified as popular.
We found that react-codemod demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.