mirror of
https://github.com/kay-is/react-from-zero.git
synced 2026-04-24 03:00:06 -04:00
93 lines
2.6 KiB
HTML
93 lines
2.6 KiB
HTML
<!doctype html>
|
|
|
|
<title>9 Component Classes - React From Zero</title>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react.js"></script>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react-dom.js"></script>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
|
|
|
|
<div id='app'></div>
|
|
|
|
<script type="text/babel">
|
|
|
|
// Often a component needs to maintain some internal state
|
|
// for example if there is interaction involved
|
|
// in this case a component function is not sufficient
|
|
// the component function can only has properties and no state
|
|
// we need a component class with a render function
|
|
var MyComponent = React.createClass({
|
|
|
|
// used for type-checking of the properties
|
|
// same as with the component function
|
|
propTypes: {
|
|
color: React.PropTypes.string,
|
|
},
|
|
|
|
// this method will be called by React
|
|
// before the components gets mounted into the DOM
|
|
getDefaultProps: function() {
|
|
return {
|
|
color: 'green'
|
|
}
|
|
},
|
|
|
|
// this method will be called by React
|
|
// before the components gets mounted into the DOM
|
|
// if this method is missing, this.state will be undefined
|
|
getInitialState: function() {
|
|
|
|
// The state can be any JavaScript value, often it is an object
|
|
return {
|
|
times: 0
|
|
}
|
|
},
|
|
|
|
// this method handles all the clicks on the <span> element
|
|
handleClick: function() {
|
|
// every call of this.setState() triggers a call of render() with the new values in this.state
|
|
this.setState({
|
|
times: this.state.times + 1
|
|
})
|
|
},
|
|
|
|
// this method will be called by React
|
|
// after the component got mounted into the DOM
|
|
// also every time this.setState() was called
|
|
// it's like the component function from before
|
|
// but without a props argument
|
|
render: function() {
|
|
|
|
// using the prop given by the creator of this component
|
|
// properties are now in this.props instead of the props argument
|
|
var style = {
|
|
color: this.props.color
|
|
}
|
|
|
|
// returning an element with a click-handler and the props and state values
|
|
// state is stored in this.state
|
|
return <span onClick={this.handleClick} style={style}>
|
|
Clicked {this.state.times} times
|
|
</span>
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// creating some instances of the interactive stateful component class
|
|
// one with default color
|
|
// Everything works exactly like with the simpler component functions
|
|
// The interface has not changed for the user of this component
|
|
var reactElement = <div>
|
|
<MyComponent color='red'/>
|
|
<br/>
|
|
<MyComponent color='blue'/>
|
|
<br/>
|
|
<MyComponent/>
|
|
</div>
|
|
|
|
var renderTarget = document.getElementById('app')
|
|
|
|
ReactDOM.render(reactElement, renderTarget)
|
|
|
|
</script>
|