Autojs设置状态栏样式

设置状态栏

在 Auto.js 中,可以通过activity.getWindow()方法来获取当前 Activity 的窗口对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
var window = activity.getWindow();
var decorView = window.getDecorView();

// 设置状态栏颜色为白色背景
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
decorView.setSystemUiVisibility(
android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
);
window.setStatusBarColor(android.graphics.Color.WHITE);
}

// 设置全屏
window.addFlags(android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN);

使用 Immer 实现在 Zustand 中更新深层对象

在开发 React 应用程序时,状态管理是一个关键问题,而 Zustand 是一个简单且强大的状态管理库,可以与 React 结合使用。在使用 Zustand 时,有时需要更新深层对象中的属性,这可能需要繁琐的手动对象复制。但是,使用 Immer 库可以简化这一过程。

首先,让我们看一下在不使用 Immer 的情况下,如何在 Zustand 中更新深层对象的属性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import create from 'zustand';

// 定义一个状态对象
const useStoreWithoutImmer = create(set => ({
user: {
name: 'Alice',
age: 30,
address: {
city: 'New York',
country: 'USA'
}
},
updateCity: newCity => set(state => ({ user: { ...state.user, address: { ...state.user.address, city: newCity } } }))
}));

export default useStoreWithoutImmer;

在上面的示例中,我们使用了展开运算符 (spread operator) 来手动创建新的对象,以更新深层对象中的属性。这种方法可能会变得繁琐,尤其是当对象结构变得更加复杂时。

现在,让我们看看如何使用 Immer 简化这一过程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import create from 'zustand';
import produce from 'immer';

// 定义一个状态对象
const useStoreWithImmer = create(set => ({
user: {
name: 'Alice',
age: 30,
address: {
city: 'New York',
country: 'USA'
}
},
updateCity: newCity => set(produce(state => {
state.user.address.city = newCity;
}))
}));

export default useStoreWithImmer;

在上面的示例中,使用了 Immer 提供的 produce 函数,这样就可以直接修改状态对象中的属性,而不必手动创建新对象。

通过以上两个示例的对比,可以清楚地看到使用 Immer 可以大大简化在 Zustand 中更新深层对象属性的过程,减少了手动对象复制的繁琐工作。

在实际项目中,选择使用 Immer 可以让代码更加简洁和易于维护,尤其是当涉及到复杂的对象结构和深层对象属性时。

希望本文能帮助你更好地理解如何使用 Immer 配合 Zustand 来更新深层对象属性,以及比较不使用 Immer 的版本。

vue自定义平滑出现指令

新建指令文件 smooth.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const DISTIANCE = 50;
const DURATION = 1000;
const animationMap = new WeakMap(); // 防止内存泄露

const ob = new IntersectionObserver((entries) => {
for (const entry of entries) {
if (entry.isIntersecting) {
const animation = animationMap.get(entry.target);
animation.play();
ob.unobserve(entry.target); // 取消监听
}
}
});

function isBelowViewport(el) {
const rect = el.getBoundingClientRect();
return rect.top > window.innerHeight;
}

export default {
mounted(el) {
if (!isBelowViewport(el)) {
return;
}

const animation = el.animate(
[
{
transform: `translateY(${DISTIANCE}px)`,
opacity: 0.5,
},
{
transform: `transformY(0)`,
opacity: 1,
},
],
{
duration: DURATION,
easing: "ease",
}
);

animation.pause();
animationMap.set(el, animation);
ob.observe(el);
},
unmounted(el) {
ob.unobserve(el);
},
};

使用:

在 main.js 中引入

1
2
import smoothDirective from "./directives/smooth";
app.directive("wu-smooth", smoothDirective);

在需要平滑上移的元素中添加指令:

1
2
3
4
<ul>
<li v-wu-smooth>666</li>
<li v-wu-smooth>6666</li>
</ul>

React学习笔记

React 学习笔记

初始化

创建项目

1
npx create-react-app <项目名称>

使用 sass

1
npm i -save-dev sass

**注意:**使用 less 需要进行额外的配置

路由 React-router

安装

1
npm i react-router-dom

**注意:**不要遗忘-dom

二级路由设置默认渲染

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const routes = createBrowserRouter([
{
path: "/",
element: <App />,
children: [
{
path: "/", // 这里将根路径 '/' 设置为要默认渲染的子路由
element: <MainComponent />,
},
{
path: "/learning1",
element: <Learnig1 />,
},
],
},
{
path: "*",
element: <NotFound />,
},
]);

集中状态管理

Zustand

安装

1
npm i zustand

使用

定义一个 store:src/store/index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
import { create } from "zustand";

const useStore = create((set) => ({
bears: 0,
increase: () =>
set((state) => ({
bears: state.bears + 1,
})),
reset: () => set({ bears: 0 }),
decrease: () => set((state) => ({ bears: state.bears - 1 })),
}));

export default useStore;

在组件中使用App.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import useStore from "@/store";

export default function App() {
// 获取bears值
const bears = useStore((state) => state.bears);
// 获取整个store
const defaultStore = useStore();
// 解构
const { bears, increase, decrease } = useStore();

return (
<div>
<small onClick={increase}>Bears值为:{bears}</small>
</div>
);
}

Redux

安装

1
npm i @reduxjs/toolkit react-redux