-
Notifications
You must be signed in to change notification settings - Fork 318
多层更新
司徒正美 edited this page Oct 1, 2017
·
2 revisions
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script type='text/javascript' src="./dist/React.js"></script>
<script src="./lib/ReactTestUtils.js"></script>
<script src="./test/babel.js"></script>
</head>
<body>
<div>开发者工具</div>
<div id='example'></div>
<script type='text/babel'>
var expect = function(a) {
return {
toBe: function(b) {
console.log(a, b, a === b)
}
}
}
class Parent extends React.Component {
constructor(props) {
super(props);
this.state = {
a: 1
};
}
render() {
console.log("Parent render")
return <Child />;
}
}
class Child extends React.Component {
constructor(props) {
super(props);
this.uuid = new Date - 0
this.state = {
a: 888
};
}
shouldComponentUpdate() {
return false;
}
render() {
console.log("Child render")
return <GrandChild />
}
}
class GrandChild extends React.Component {
render() {
console.log("GrandChild render")
return <div><span>{new Date() - 0}</span><b>1111</b></div>;
}
}
var container = document.getElementById('example')
var s = ReactDOM.render(<Parent />, container);
s.setState({a:2})
console.log(s)
</script>
</body>
</html>
更新
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script type='text/javascript' src="./dist/React.js"></script>
<script src="./lib/ReactTestUtils.js"></script>
<script src="./test/babel.js"></script>
</head>
<body>
<div>开发者工具</div>
<div id='example'></div>
<script type='text/babel'>
var expect = function(a) {
return {
toBe: function(b) {
console.log(a, b, a === b)
}
}
}
class Parent extends React.Component {
constructor(props) {
super(props);
this.state = {
a: 1
};
}
render() {
console.log("Parent render")
return <Child a={this.state.a} />;
}
}
class Child extends React.Component {
constructor(props) {
super(props);
this.uuid = new Date - 0
console.log(props)
this.state = {
a: props.a
};
}
componentWillReceiveProps(props){
this.state.a = props.a
}
render() {
console.log("Child render",this.state.a)
return this.state.a ? <GrandChild />: <Another/>
}
}
class Another extends React.Component {
render() {
console.log("Another render")
return <em>Another</em>;
}
}
class GrandChild extends React.Component {
render() {
console.log("GrandChild render")
return <div><span>{new Date() - 0}</span><b>1111</b></div>;
}
}
var container = document.getElementById('example')
var s = ReactDOM.render(<Parent />, container);
console.log("==============")
s.setState({a:0})
console.log(s)
</script>
</body>
</html>
``html
<script type='text/javascript' src="./dist/React.js"></script> <script type='text/javascript' src="./test/react.development.js"></script> <script type='text/javascript' src="./test/react-dom.development.js"></script><script type='text/javascript' src="./lib/babel.js"></script>
<pre>应该等于
</pre>
<h1 id='root' class="root">
</h1>
<script type='text/babel'>
var container = document.getElementById("example")
var div = container
var PropTypes = React.PropTypes
if(!window.ReactDOM){
window.ReactDOM = React
}
var expect = function(a) {
return {
toBe: function(b) {
console.log(a,"vs", b, a === b)
},
toEqual(b){
console.log(a,"vs", b, a +""=== b+"")
},
toThrow(b){
try{
a()
}catch(e){
console.log(e,"vs", b, e.message +""=== b+"")
}
}
}
}
const Hello = ({ name }) => <h1>Hello {name}!</h1>;
class ShowMyError extends React.Component {
constructor(props) {
super(props);
this.state = { error: false };
}
componentDidCatch(error, info) {
console.log(error, info)
this.setState({ error, info });
}
render() {
if (this.state.error) {
return (
<div>
<h1>
Error AGAIN: {this.state.error.toString()}
</h1>
{this.state.info &&
this.state.info.componentStack.split("\n").map(i => {
return (
<div key={i}>
{i}
</div>
);
})}
</div>
);
}
return this.props.children;
}
}
class Broken extends React.Component {
constructor(props) {
super(props);
this.state = { throw: false, count: 0 };
}
render() {
if (this.state.throw) {
throw new Error("YOLO");
}
return (
<div>
<button
onClick={e => {
this.setState({ throw: true });
}}
>
button will render error.
</button>
<button onClick={e => {
this.setState(({ count }) => ({
count: count + 1
}));
}}>button will not throw</button>
<div>
{"All good here. Count: "}{this.state.count}
</div>
</div>
);
}
}
class App extends React.Component {
render() {
const styles = {
fontFamily: "sans-serif",
textAlign: "center"
};
return (
<div style={styles}>
<Hello name="ShowMyError" />
<h2>
Start clicking to see some {"\u2728"}magic{"\u2728"}
</h2>
<ShowMyError>
<Broken />
</ShowMyError>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));