我发现有几种方法可以处理带有钩子的用户文本输入。用钩子处理输入的更优选或更合适的方法是什么?您会使用哪个?
1)最简单的钩子来处理输入,但是您拥有更多的字段,必须编写更多的重复代码。
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
Run Code Online (Sandbox Code Playgroud)
事件:
onChange={event => setPassword(event.target.value)}
onChange={event => setUsername(event.target.value)}
Run Code Online (Sandbox Code Playgroud)
2)与上面的示例类似,但具有动态键名
const [inputValues, setInputValues] = useState({
username: '', password: ''
});
const handleOnChange = event => {
const { name, value } = event.target;
setInputValues({ ...inputValues, [name]: value });
};
Run Code Online (Sandbox Code Playgroud)
事件:
onChange={handleOnChange}
Run Code Online (Sandbox Code Playgroud)
3)通常,最好使用替代方法useState,如ReactJS文档所述。useReduceruseState
const [inputValues, setInputValues] = useReducer(
(state, newState) => ({ ...state, ...newState }),
{username: '', password: ''}
);
const handleOnChange = event => {
const …Run Code Online (Sandbox Code Playgroud) 我有一个用于分页响应的通用接口:
export interface PaginatedResponse<T> {
pageIndex: number;
pageSize: number;
totalCount: number;
totalPages: number;
items: Array<T>;
}
Run Code Online (Sandbox Code Playgroud)
然后我想将其转换为 zod 模式以进行运行时类型检查。方法是这样的:
const PaginatedResponseSchema = z.object({
pageIndex: z.number(),
pageSize: z.number(),
totalCount: z.number(),
totalPages: z.number(),
items: z.array(???), // <=
});
export type PaginatedResponse<T> = z.infer<typeof PaginatedResponseSchema>;
Run Code Online (Sandbox Code Playgroud)
什么类型的数组应该是架构中的项目?
例如,有
.gitlab-cy.yml与ENV_BACKEND_URI可变
build:
stage: build
variables:
ENV_BACKEND_URI: "http://localhost:4200"
script:
- docker-compose build
Run Code Online (Sandbox Code Playgroud)
docker-compose 用途 dockerfile
FROM node:10-alpine as build-stage
...
...
...
RUN ["chmod", "+x", "/dummy.sh"]
...
...
Run Code Online (Sandbox Code Playgroud)
我想ENV_BACKEND_URI在dummy.sh脚本中使用这个变量
例如只是为了回应它
echo $ENV_BACKEND_URI
Run Code Online (Sandbox Code Playgroud)
我如何通过它?
我已经尝试将其设置 docker-compose.yml
environment:
- ENV_BACKEND_URI=${ENV_BACKEND_URI}
Run Code Online (Sandbox Code Playgroud)
但它在dockerfile也不可用dymmy.sh
我的 Angular 应用程序中有enviroment.prod.ts文件
export const environment = {
backendUrl: '127.0.0.1:3000',
};
Run Code Online (Sandbox Code Playgroud)
这是docker-compose.prod.yml的一部分:
....
environment:
- ENV_BACKEND_URI=127.0.0.1:3000
....
Run Code Online (Sandbox Code Playgroud)
如何设置backendUrl使用ENV_BACKEND_URIdocker -compose.prod.yml?我想要这样的东西:
export const environment = {
backendUrl: '${ENV_BACKEND_URI}',
};
Run Code Online (Sandbox Code Playgroud)
这可能吗?
我正在使用 gin 来处理请求,但是我在验证请求正文中的数据时遇到问题,例如
type User struct {
Username string `json:"username" binding:"required,min=1,max=16"`
Name string `json:"name" binding:"required,min=1,max=16"`
Password string `json:"password" binding:"required,min=1,max=16"`
}
func loginHandler(ctx *gin.Context) {
var user User
if err := ctx.ShouldBindJSON(&user); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
}
Run Code Online (Sandbox Code Playgroud)
如何处理带有空格的值,例如" username "或" John Doe "?据我所知,不可能在 gin 的验证器中使用正则表达式。Golang 中请求正文验证的最佳实践或模式是什么?
这是 minikube Kubernetes 的清单文件,用于部署和服务:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-deployment
spec:
selector:
matchLabels:
app: hello
replicas: 3
template:
metadata:
labels:
app: hello
spec:
containers:
- name: hello
image: hello_hello
imagePullPolicy: Never
ports:
- containerPort: 4001
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: hello
spec:
selector:
app: hello
ports:
- port: 4001
nodePort: 30036
protocol: TCP
type: NodePort
Run Code Online (Sandbox Code Playgroud)
以及一个用 Golang 编写的简单 HTTP 服务器
package main
import (
http "net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/ping", …Run Code Online (Sandbox Code Playgroud) go ×2
angular ×1
docker ×1
gitlab-ci ×1
http-post ×1
json ×1
kubernetes ×1
react-hooks ×1
reactjs ×1
request ×1
runtime ×1
typescript ×1
validation ×1
zod ×1