在阅读了React set focus和React focus with timeout之后,我仍然有点困惑什么是设置focus输入的正确跨浏览器选项。
我遇到了条件渲染输入的问题,这些输入在渲染后可能具有焦点table,其中input放置在 中td。
1)使用componentDidUpdate的解决方案:
//...
componentDidUpdate() {
this.input && this.input.focus();
}
render() {
const show = this.state.isShown;
return (
<td className="editable">
{!show ? (
<input
type="number"
ref={(input) => { this.input = input }}
value={this.state.value}
onBlur={this.save}
onKeyPress={e =>
e.which === 13 || e.keyCode === 13 ? this.save() : null}
/>
) : (
<span onClick={this.onTdClick}>{this.props.name}</span>
)}
</td>
);
}
Run Code Online (Sandbox Code Playgroud)
此解决方案适用于 Chrome、IE11、Edge,但不适用于最新的 Firefox( …
我想创建一个通用函数,它将接受一个对象,然后进行一些转换并返回具有相同键和不同值的新对象。我试图使它成为“强类型”,因此使用它的每个人都将受益于 TS 并且不存在的键应该抛出错误。
我现在所拥有的:
const hash = {
"first": 1,
"second": 2,
"third": 3,
}
type Mapper<T> = {
[key in keyof T]: number
}
type Result<T>= {
[key in keyof T]: () => number
}
const transform = <T>(mapper: Mapper<T>) => {
const result = {} as Result<T>
(Object.keys(mapper) as (keyof T)[]).map(key => {
result[key] = () => mapper[key]
})
return result
}
type Hash = typeof hash
const a = transform<Hash>(hash)
a.first()
// a.fifth() OK error
Run Code Online (Sandbox Code Playgroud)
它运作良好,但我正在寻找解决方案来解决这个问题:
删除类型断言 …
javascript typescript typescript-generics typescript-typings