• Skip to content
  • Skip to primary sidebar

Developers Tips & Tricks

You are here: Home / React JS / React PropTypes

React PropTypes

26 abril, 2016 by MPezzolano

When you create a class with React, using the createClass method.
We can use some properties and methods in order to manage behavior of our class.

for this let’s use points : propTypes and getDefaultProps.

propTypes : We can set the properties of the class.

getDefaultProps: We can set default values.

So we can set the components properties.

  • React.PropTypes.array
  • React.PropTypes.bool
  • React.PropTypes.func
  • React.PropTypes.number
  • React.PropTypes.object
  • React.PropTypes.string

 

PropTypes with React

<html>
<head>
    <title>Props With ReactJS</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.0/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.0/JSXTransformer.js"></script>
</head>
<body>
<div id="content"></div>
<script type="text/jsx">
    var App = React.createClass({
    propTypes: {
    optionalArray: React.PropTypes.array,
    optionalBool: React.PropTypes.bool,
    optionalFunc: React.PropTypes.func,
    optionalNumber: React.PropTypes.number,
    optionalObject: React.PropTypes.object,
    optionalString: React.PropTypes.string
},
    getDefaultProps: function() {
    return {
    optionalArray: [],
    optionalBool: true,
    optionalFunc: function(){},
    optionalNumber: 1,
    optionalObject: {},
    optionalString: 'default value'
};
},
    render: function() {
    return (
    <div>
    <input
    type="text"
    optionalArray={this.props.optionalArray}
    optionalBool={this.props.optionalBool}
    optionalFunc={this.props.optionalFunc}
    optionalNumber={this.props.optionalNumber}
    optionalObject={this.props.optionalObject}
    optionalString={this.props.optionalString}
    />
    </div>
    );
}
});
    React.render(<App />, document.body);
</script>
</body>
</html>


 

 

 

Filed Under: HTML5, JavaScript, React JS Tagged With: createClass, getDefaultProps, PropTypes, react

Primary Sidebar

Categorías

  • Angular JS (10)
  • BackBone JS (6)
  • Ember JS (2)
  • Express JS (4)
  • EXTJS (1)
  • General (7)
  • GIT (3)
  • Grunt JS (2)
  • HTML5 (8)
  • JAVA (5)
  • JavaScript (14)
  • JQUERY (15)
  • JSON (1)
  • JSP (1)
  • Knockout JS (2)
  • LARAVEL (13)
  • Linux (2)
  • Mobile Angular UI (1)
  • Modulus.io (1)
  • MongoDB (5)
  • MySQL (3)
  • NodeJS (11)
  • PHP (1)
  • Polymer (3)
  • PostgreSQL (1)
  • Prism.JS (1)
  • PYTHON (10)
  • QOOXDOO (1)
  • React JS (7)
  • Redis (2)
  • Sin categoría (6)
  • SQL (1)
  • TWIG (4)
  • UnderScore (1)
  • Web Services. (2)
  • Wordpress (1)

Cloud Tags

Angular JS BackBone JS Ember JS Express JS EXTJS General GIT Grunt JS HTML5 JAVA JavaScript JQUERY JSON JSP Knockout JS LARAVEL Linux Mobile Angular UI Modulus.io MongoDB MySQL NodeJS PHP Polymer PostgreSQL Prism.JS PYTHON QOOXDOO React JS Redis Sin categoría SQL TWIG UnderScore Web Services. Wordpress

Copyright © 2023 · Genesis Sample Theme on Genesis Framework · WordPress · Log in