我正在使用react-hook-form。我正在尝试使用这个库实现购物车,但在获取数值方面遇到了困难。使用 getValues("count")时,即使设置type="number", name="count"始终返回字符串值的输入标记。
对于name="count"的字符串值,我的自定义函数增量无法按预期工作。当我单击 + 按钮时,它返回并添加了字符串。
import React, { FC, useEffect } from 'react';
import * as Styled from './style';
import { useForm, ErrorMessage} from 'react-hook-form';
import { ItemCountForm } from 'src/models/item-count';
export const CountForm:FC<{
partialData : Omit<ItemCountForm,'count'>
}> = ({
partialData
}) => {
const { register, handleSubmit, errors,getValues, setValue } = useForm<{count:number}>({
defaultValues:{
count: 1}
});
const onSubmit = async(data: {count: number}) => {
console.log('submitted');
const formData: ItemCountForm = { …Run Code Online (Sandbox Code Playgroud) I want to pass action.url string as a parameter of topicDummy function which return Promise, but It keeps show me
No overload matches this call.
The last overload gave the following error.
Argument of type '<TopicData>(url: string) => Promise<TopicData>' is not assignable to parameter of type '{ context: unknown; fn: (this: unknown, ...args: any[]) => any; }'.
Type '<TopicData>(url: string) => Promise<TopicData>' is missing the following properties from type '{ context: unknown; fn: (this: unknown, ...args: any[]) => any; }': …Run Code Online (Sandbox Code Playgroud) 我想使用“useSelector”来选择 rootStore 的正确状态,但无法正确获取状态。原因是RootState的主题缩减器从未给我类型
export type RootState = ReturnType<typeof rootReducer>
Run Code Online (Sandbox Code Playgroud)
如何正确获取主题类型?
整个代码看起来像
///topic.ts
import { Action, ActionCreator } from 'redux';
import { CardData } from '@models/card';
import { TopicName } from 'src/models/topic';
import { ActionEnum } from './index';
//action
export enum TopicEnum {
FETCH_TOPIC = "FETCH_TOPIC"
}
type TopicType = ActionEnum | TopicEnum;
export interface TopicBasicState{
topic: TopicName
isLoading :boolean
data: CardData[]
isError: boolean
}
export const initialState: TopicBasicState = {
topic: 'all',
isLoading: true,
data: [] …Run Code Online (Sandbox Code Playgroud) 一些 React 组件将className属性传递给子组件。我没有看到任何需要className在我的项目中作为支柱传递。
className为什么使用这个道具?我什么时候应该采用这种方法?
import styled from 'styled-components'
const ReactComponent = ({ className }) => {
return <div className={className}/>
}
const StyledComponent = styled(ReactComponent)``;
Run Code Online (Sandbox Code Playgroud) 我是打字稿初学者。
Pick和 T["properties"] 索引引用有什么区别?
例如:
interface Generic {
red: number
apple: string;
}
type RedType = Pick<Generic, "red">
type RedType2 = Generic["red"]
Run Code Online (Sandbox Code Playgroud) 这是我的代码结构
const StyledArticle = styled.article``;
const StyledDiv = styled.div``;
<StyledArticle>
<StyledDiv>1</StyledDiv>
<StyledDiv>2</StyledDiv>
<StyledDiv>3</StyledDiv>
<StyledDiv>4</StyledDiv>
</StyledArticle>
Run Code Online (Sandbox Code Playgroud)
除了第 4 个 StyledDiv,我如何添加 css StyledDiv 1,2,3。
无需在 StyledArticle 中添加任何 css 语句
没有 js,jquery 只使用 css。