注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
本帖最后由 lglanan2023 于 2023-10-20 18:02 编辑
. 1point3acres
1. 读代码,react中settimeout里调用setState,state如何变化。
(react中的过期闭包问题:指因为闭包的生命周期长于其引用的变量的生命周期而导致的问题。)- import React from 'react'
复制代码 2. 读代码,react组件函数中遇到while(true)死循环,页面会怎样?(大计算导致渲染阻塞;)
. Waral dи,3. promise相关题目,图片批量加载,超出最大加载限制进入等待。
a. promise.all中,如果一个任务失败了,则全部任务失败。promise.allSettled,如果一个任务失败,也会等待所有任务都执行完成。- import './style.css';
- const urls = [
- 'https://resources.matcha-jp.com/resize/720x2000/2020/04/23-101958.jpeg',. Χ
- 'https://twgreatdaily.com/images/elastic/54y/54yt93ABiuFnsJQVYn-e.jpg',
- 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRil2a9Sy8VA3VcaMXSkHhfY4JwEJA2cH60axa1zjZQnqlkoVgH&usqp=CAU',
. ---- - 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRnYzT9n2O334O9l0pNC6_OIDEZDCdPDgD0BeYbN3AYrF_AWKtG&usqp=CAU',. Χ
- 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSg59FBCFkElCSPCSZeVz8asTpX1z49qsIeydd3_wqZxHtpN2dR&usqp=CAU',. 1point 3 acres
- 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQ4F4ULhP6M08kTWCUE2csMKS-55y5-uZ1LJ3Bi_l9yob8gcUmP&usqp=CAU',
- 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTE-vlPXWnLfef967S-4eN0q6c81JPkkp-SvKqKATbeo5FwU6eb&usqp=CAU',
- 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTNspT9VbqTH9kgG3830MSQVsxX0FV8ZxzU8Gk5cNhzH9pDdxGR&usqp=CAU',
- 'https://static.newmobilelife.com/wp-content/uploads/2016/07/0wOSIGK-1024x576.jpg',.--
- ];. .и
- const sleep = (ms: number) =>
- new Promise((resolve) => {. check 1point3acres for more.
- window.setTimeout(resolve, ms);
- });. ----
- const insertImg = async (url: string) => {
. From 1point 3acres bbs - return new Promise((resolve, reject) => {
- const img = document.createElement('img');
- img.width = 200;
- img.height = 200;
- img.src = url;
- . From 1point 3acres bbs
- img.addEventListener('load', async () => {
- await sleep(2000); // Just for testing purpose and you can ignore it.
- document.body.appendChild(img);
- resolve(url);
- });
- img.addEventListener('error', () => {
- reject();
- });
- });
- };
- /**
- * Write a main function to ...
- */
- function async sequenceRender(urls: string[]):void {
- const queue = [];. 1point3acres.com
- for (const url of urls) {
- if (queue.length < 2) {
- const p = insertImg(url);
- queue.push(p)
- } else {. Waral dи,
- try {
- await Promise.allSettled(queue);
- } catch (err) {
- console.log(err);
- }
-
- }. 1point3acres
-
- }
- }
复制代码 4. 代码题:根据id找到子节点并返回完整路径- class TreeNode {
- id: string;
. Waral dи, - children?: [];
- constructure(id: string): void {
- this.id = id;
- }
- }
- function getPath(tree: TreeNode, id: string): string[] {
- if (tree.id === id) {
- return [tree.id];
- }
- if (tree.children) {. 1point 3 acres
- for (const child of tree.children) {
- let ans = getPath(child, id);
- if (ans.length > 0) {
- // 这里找到子节点存在目标,返回父节点id
- return [tree.id, ...ans];
- }. Waral dи,
- }
- }
- return [];
- }
- // 当时的代码.--
- type Tree = {
- id: string;.--
- children?: Tree[];
- };
- . From 1point 3acres bbs
- const tree: Tree = {.--
- id: '1',
- children: [
- {
- id: '2',
- children: [
- {
- id: '3',
- },
- ],
- },
- {
- id: '4',
- },
- {
- id: '5',. Χ
- children: [.1point3acres
- {
- id: '6',
- },
- {
- id: '7',
.google и - children: [
- {
- id: '8',
- },-baidu 1point3acres
- {.
- id: '9',
- },
- ],
- },
- ],
- },.--
- ],.1point3acres
- };
- .1point3acres
- /** ..
- * @example
- * getPath(tree, '2') ==> ['1', '2']
- * getPath(tree, '3') ==> ['1', '2', '3']
- * getPath(tree, '9') ==> ['1', '5', '7', '9'].google и
- * getPath(tree, '10') ==> []
- */
- const getPath = (tree: Tree, id: string) => {
- const temp: string[] = [];
- const result = [].1point3acres
- function findNode(node: Tree, id: string) {
- result.push(node.id);. 1point 3acres
- if (node.id !== id && !node.children) {
- return;
- }
- if (node.id === id) {
- result = temp;
- };
- if (node.children) {. ----
- for (let child of node.children) {
- findNode(child, id);
- result.pop();
- }
- }
- }.--
- findNode(tree, id);
- return result;
- };
- // document.body.innerHTML = `The path is: ${getPath(tree, '9‘).join(', ')}`;
复制代码 5. 项目中的技术点:
a. 大数据渲染优化
b. 组件库建设,意义。
求米,谢谢大家~~ |