我是一个新手,用react 打字,try 了很多东西,但都不起作用.状态不起作用并获取类型脚本错误类型"userContextType"上不存在属性"状态和调度"|空' UserContRed.tsx
export const UserContRed = () => {
//const context = useContext(ContRedContext)
const {state , dispatch } = useContext(ContRedContext)
const [firstname, setFirstname] = useState('')
const [id, setId ] = useState(0)
const [lastname, setLastname] = useState('')
// let displayUsers = context?.state.users.map( (u:any) => {
let displayUsers = state.users.map( (u:any) => {
return(
<ul key={u.id}>
<li>{u.id}</li>
<li>{u.firstname}</li>
<li>{u.lastname}</li>
</ul>
)
})
const addUser = () => {
const addUser = () => dispatch({type:ActionType.ADD, payload : {id:1,firstname:firstname, lastname:lastname}})
}
return(
<div>
<div>
<label>Id: </label>
<input type="text" onChange={(e)=>{setId(Number(e.target.value))}} />
</div>
<div>
<label>Firstname: </label>
<input type="text" onChange={(e)=>{setFirstname(e.target.value)}} />
</div>
<div>
<label>Lastname: </label>
<input type="text" onChange={(e)=>{setLastname(e.target.value)}} />
</div>
<button onClick={addUser}>sendToStore</button>
</div>
)
}
ContRed.tsx
export type User = {
id: number;
firstname: string;
lastname: string;
}
type contredProps = {
children: React.ReactNode
}
export type IuserState = {
users : User[]
}
const initialState: IuserState = {
users : [
{
id:0,
firstname:'',
lastname: ''
}
]
}
export enum ActionType {
ADD="add",
REMOVE="remove"
}
type userAction = {
type: ActionType,
payload: User
}
export const reducer = (state: IuserState, action: userAction) : typeof initialState => {
switch(action.type){
case "add":
return ({...state, users: [...state.users,
{id:action.payload.id,
firstname:action.payload.firstname,
lastname:action.payload.lastname
}
]
})
case "remove":
return ({...state, users: [
...state.users.filter(data => data.id !== action.payload.id)]
})
default:
return state;
}
}
type UserContextType = {
state: IuserState
dispatch: React.Dispatch<userAction>
}
export const ContRedContext = createContext<UserContextType|null>({
state:initialState,
dispatch: () => {}
})
export const ContRedContextProvier = ({children}:contredProps) => {
const[state,dispatch] = useReducer(reducer,initialState as IuserState);
const value : UserContextType = { state, dispatch}
return(
<ContRedContext.Provider value={value}>
{children}
</ContRedContext.Provider>
)
}
App.tsx
<ContRedContextProvier>
<UserContRed/>
</ContRedContextProvier>
我try 使用常数而不是go struct 化类型脚本错误消失,但它不会添加或更新状态.在我看来,Typescript 太难了,请解释或指导我做错了什么. 下面try 的Typescript 很好,但功能不起作用
const context = useContext(ContRedContext)
let displayUsers = context?.state.users.map( (u:any) => { ...}
const addUser = () => context?.state.dispatch({type:ActionType.ADD, payload : {id:1,firstname:firstname, lastname:lastname}})
try 过:
const context = useContext(ContRedContext)
let displayUsers = context?.state.users.map( (u:any) => { ...}
const addUser = () => context?.state.dispatch({type:ActionType.ADD, payload : {id:1,firstname:firstname, lastname:lastname}})
预期: 用户应该添加到状态并在子组件中呈现.