Skip to content

Commit b0400ce

Browse files
committed
update: --
1 parent 8b6f136 commit b0400ce

File tree

4 files changed

+122
-1
lines changed

4 files changed

+122
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@
252252
- [JSX到JavaScript的转换](./react/JSX到JavaScript的转换.md)
253253
- [ReactElement](./react/ReactElement.md)
254254
- [react-component](./react/react-component.md)
255-
255+
- [react-ref](./react/react-ref.md)
256+
- [forward-ref](./react/forward-ref.md)
257+
- [context](./react/context.md)
256258

257259

258260

react/context.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Context 两种方式
2+
3+
1. childContentType
4+
2. createContext
5+
6+
```js
7+
import React from 'react'
8+
import PropTypes from 'prop-types'
9+
10+
const { Provider, Consumer } = React.createContext('default')
11+
12+
class Parent extends React.Component {
13+
state = {
14+
ChildContext: '123',
15+
newContext: '456',
16+
}
17+
18+
getChildContext() {
19+
return { value: this.state.childContext }
20+
}
21+
22+
render() {
23+
return (
24+
<>
25+
)
26+
}
27+
}
28+
```

react/forward-ref.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## forwardRef
2+
3+
```js
4+
// 获取某个节点的实例
5+
import React from 'react'
6+
7+
const TargetCompont = React.forwardRef((props, ref) => {
8+
<input type="text" ref={ref}/>
9+
})
10+
11+
export default class Comp extends React.Component {
12+
constructor() {
13+
super()
14+
this.ref = React.createRef();
15+
}
16+
17+
componentDidMount() {
18+
this.ref.current.value = 'ref get input'
19+
}
20+
21+
render() {
22+
return <TargetCompont ref={this.ref} />
23+
}
24+
}
25+
```
26+
27+
```js
28+
export default function forwardRef<Props, ElementType:>
29+
30+
return {
31+
$$typeof: REACT_FORWARD_REF_TYPE,
32+
render,
33+
};
34+
```

react/react-ref.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## createRef 和 ref
2+
3+
> ref的三种使用方式
4+
5+
string ref
6+
7+
function
8+
9+
createRef
10+
11+
```js
12+
13+
constructor() {
14+
super()
15+
this.objRef = React.createRef()
16+
17+
// {current: null}
18+
}
19+
20+
componentDidMount() {
21+
setTimeout(() => {
22+
// {this.refs.ref1.textContent}
23+
// {this.ref2.textContent}
24+
// {this.ref3.textContent}
25+
this.refs.stringRef.textContent = 'striing ref got'
26+
this.methodRef.textContent = 'method ref got'
27+
this.objRef.current.textContent = 'obj ref got'
28+
}, 1000)
29+
}
30+
31+
render() {
32+
return (
33+
<>
34+
<p ref="stringRef">span1</p>
35+
<p ref={ele => (this.methodReef = ele)}>span3</p>
36+
<p ref={this.objRef}>span3</p>
37+
38+
</>
39+
)
40+
}
41+
```
42+
43+
44+
```js
45+
import type {RefObject} from 'shared/ReactTypes';
46+
47+
export function createRef(): RefObject {
48+
const crfObject = {
49+
current: null,
50+
};
51+
if(__DEV__) {
52+
Object.seal(refObject);
53+
}
54+
return refObject;
55+
}
56+
```
57+

0 commit comments

Comments
 (0)