[React] React Fundamentals: Introduction to Properties
var React_app = React.createClass({ render: function() { var age = this.props.age; var name = this.props.name; var isMan = this.props.isMan ? 'Man' : 'Woman'; return ( <div> <b>Should have (), if you have mutli tag in return, include them into a div</b> <h1>Hello React World!</h1> <h2>Show num: {age}, name: {name}, sex:{isMan}</h2> </div> ); } });
You can set default properties:
var React_app = React.createClass({ getDefaultProps: function() { return { age: 0, name: "Who?", isMan: true } }, render: function() { var age = this.props.age; var name = this.props.name; var isMan = this.props.isMan ? 'Man' : 'Woman'; return ( <div> <b>Should have (), if you have mutli tag in return, include them into a div</b> <h1>Hello React World!</h1> <h2>Show num: {age}, name: {name}, sex:{isMan}</h2> </div> ); } });
You can set the type of props:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>React Lesson 2: Prop</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/JSXTransformer.js"></script>
<script type="text/jsx">
var React_app = React.createClass({
getDefaultProps: function() {
return {
age: 0,
name: "Who?",
isMan: true
}
},
propTypes: {
name: React.PropTypes.string.isRequired,
age: React.prototype.number,
isMan: React.prototype.bool
},
render: function() {
var age = this.props.age;
var name = this.props.name;
var isMan = this.props.isMan ? 'Man' : 'Woman';
return (
<div>
<b>Should have (), if you have mutli tag in return, include them into a div</b>
<h1>Hello React World!</h1>
<h2>Show num: {age}, name: {name}, sex:{isMan}</h2>
</div>
);
}
});
React.render(<React_app name="John"/>, document.body);
</script>
</body>
</html>

浙公网安备 33010602011771号