chunbin1 / blog Goto Github PK
View Code? Open in Web Editor NEWgit actions + dumijs 写的博客
Home Page: https://chunbin1.github.io/blog/
git actions + dumijs 写的博客
Home Page: https://chunbin1.github.io/blog/
需求:一个评论用的窗口,通过forwardRef传递方法给父组件调用
export type CommentHandler = {
openDrawer: () => void
closeDrawer: () => void
}
const CommentDrawer: React.ForwardRefRenderFunction<CommentHandler, IProps> = (props,ref)=>{
const openDrawer = ()=>{};
const closeDrawer = ()=>{};
useImperativeHandle(ref, () => {
return {
openDrawer,
closeDrawer,
}
})
return <div></div>
}
export default forwardRef(CommentDrawer)
使用
const ref = useRef<React.ElementRef<typeof CommentDrawer>>(null)
ref.current?.openDrawer()
思路:
利用Map有顺序的原理
/**
* @param {number} capacity
*/
var LRUCache = function(capacity) {
this.capacity = capacity;
this.map = new Map();
};
/**
* @param {number} key
* @return {number}
*/
LRUCache.prototype.get = function(key) {
if(this.map.has(key)){
let temp=this.map.get(key)
this.map.delete(key);
this.map.set(key, temp);
return temp
}else{
return -1
}
};
/**
* @param {number} key
* @param {number} value
* @return {void}
*/
LRUCache.prototype.put = function(key, value) {
if(this.map.has(key)){
this.map.delete(key);
}
this.map.set(key,value);
if(this.map.size > this.capacity){
this.map.delete(this.map.keys().next().value);
}
};
/**
* Your LRUCache object will be instantiated and called as such:
* var obj = new LRUCache(capacity)
* var param_1 = obj.get(key)
* obj.put(key,value)
*/
使用Content-Type: multipart/x-mixed-replace;
可以实现服务端的主动推送
常用于函数的返回值,判断类型是否是某种类型
function isString(test: any): test is string {
return typeof test === 'string'
}
function example(foo: any) {
if (isString(foo)) {
console.log('it is a string' + foo)
// 在这里 foo的类型已经变成了string,ts编译器不会报错
console.log(foo.length) // string function
} else {
console.log('not string')
}
}
example('hello world')
example(2)
function App() {
const [num, updateNum] = useState(0);
function increment() {
setTimeout(() => {
updateNum(num + 1);
}, 1000);
}
return <p onClick={increment}>{num}</p>;
}
快速点击5次,视图上显示多少?
答案:1
因为视图还没更新,所以num还是0,所以updateNum
updateNum(num=>num+1)
git checkout -b <new_branch> [<start_point>] // 从start_point checkout出new_branch
git checkout <branch> <location> // 用branch分支下的location目录覆盖当前的location
求输出结果
1.
class App extends React.Component{
state = { number:0 }
componentDidMount = ()=>{
this.setState({ number:this.state.number + 1 },()=>{ console.log( 'callback1', this.state.number) })
console.log(this.state.number)
this.setState({ number:this.state.number + 1 },()=>{ console.log( 'callback2', this.state.number) })
console.log(this.state.number)
this.setState({ number:this.state.number + 1 },()=>{ console.log( 'callback3', this.state.number) })
console.log(this.state.number)
}
render(){
return <div>
{ this.state.number }
</div>
}
}
输出:
0 0 0 callbackl1 1 callback2 1 callback3 1
参考:链接 -- 需要翻墙
import React, { useRef, useEffect } from 'react';
const SimpleCanvasExample: React.FC<{}> = () => {
let canvasRef = useRef<HTMLCanvasElement | null>(null);
let canvasCtxRef = React.useRef<CanvasRenderingContext2D | null>(null);
useEffect(() => {
// Initialize
if (canvasRef.current) {
canvasCtxRef.current = canvasRef.current.getContext('2d');
let ctx = canvasCtxRef.current;
ctx!.beginPath();
ctx!.arc(95, 50, 40, 0, 2 * Math.PI);
ctx!.stroke();
}
}, []);
return <canvas ref={canvasRef}></canvas>;
};
export default SimpleCanvasExample;
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.