Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
react-class
Advanced tools
A carefully crafted base class for all your React components
$ npm install react-class --save
Instead of extending React.Component
you have to extend the class exported by react-class
.
import Component from 'react-class'
class MyApp extends Component {
render(){
// you have to call prepareProps in order to get defaultClassName
// and defaultStyle applied to props
var props = this.prepareProps(this.props)
return <div {...props} onClick={this.onClick}>
//onClick is auto-bound to "this", so you can keep your code dry
</div>
}
onClick(){
console.log(this)
}
}
MyApp.defaultProps = {
defaultStyle: {
border: '2px solid red'
},
defaultClassName: 'myapp'
}
So you can use <MyApp style={{color: 'blue'}} className="main" />
and get defaultProps.defaultClassName
always applied to your component and defaultProps.defaultStyle
merged into props.style
. Of course, any colliding style you specify in props.style
will override the one in defaultProps.defaultStyle
The result of
<MyApp style={{color: 'blue'}} className="main" />
is a div with the following:
<div style="color: blue; border: 2px solid red" class="myapp main">
</div>
To get defaultProps.defaultStyle
and defaultProps.defaultClassName
applied on the props object, remember to call prepareProps
var props = this.prepareProps(this.props)
All it does is the following:
function prepareProps(thisProps){
var props = assign({}, thisProps)
props.style = assign({},
this.contructor.defaultProps.defaultStyle, props.style
)
props.className = (props.className || '') + ' ' +
(this.constructor.defaultProps.defaultClassName || '')
return props
}
In order to get autobinding, just extend the class exported by react-class
var ReactClass = require('react-class');
class App extends ReactClass { ... }
Generally you want your components to have a default style (of course, which can be overriden).
Very often you also want a default className
to be applied all the time to your components, no matter if the user of your components passes a className
attribute or not in the props.
Also, autobinding is a nice feature!
react-class
is a very thin layer around React.Component
, so just in case you decide removing it in the future, you'll be safe and will only have to do very minor code changes.
We're not doing anything magical!
FAQs
A carefully crafted base class for all your React components
The npm package react-class receives a total of 8,424 weekly downloads. As such, react-class popularity was classified as popular.
We found that react-class demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.