我按照这个例子关于redux
,但我用TypeScript编写。
https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/todos-with-undo
我在AddTodo.tsx
档案中遇到了麻烦。
import * as React from 'react'
import { connect } from 'react-redux'
import { addTodo } from '../actions'
const AddTodo = ({ dispatch }) => {
let input: any
return (
<div>
<form onSubmit={e => {
e.preventDefault()
if (!input.value.trim()) {
return
}
dispatch(addTodo(input.value))
input.value = ''
}}>
<input ref={node => {
input = node
}} />
<button type="submit">
Add Todo
</button>
</form>
</div>
)
}
AddTodo = connect()(AddTodo)
export default AddTodo
不允许使用dispatch
参数。错误说:
[ts]绑定元素&#39; dispatch&#39;隐含地有一个“任何”的类型。
答案 0 :(得分:2)
可能是类型问题。尝试在道具中添加类型:
import * as React from 'react'
import { connect } from 'react-redux'
import { addTodo } from '../actions'
import { Dispatch } from 'redux';
interface AddTodoProps {
dispatch : Dispatch<{}>
}
const AddTodo = (props : AddTodoProps) => {
let input: any
return (
<div>
<form onSubmit={e => {
e.preventDefault()
if (!input.value.trim()) {
return
}
props.dispatch(addTodo(input.value))
input.value = ''
}}>
<input ref={node => {
input = node
}} />
<button type="submit">
Add Todo
</button>
</form>
</div>
)
}
export default connect()(AddTodo)