0

最初我想知道:

我该如何为此编写处理程序?

type state = string;
type action = | ChangeName(string)
let reducer = (_state, action) => switch action { | ChangeName(text) => text }
[@react.component]
let make = () => {
    let (state, dispatch) = React.usefReducer(reducer, "");
     /* What is the parameter type and how do I decode it? */
    let onChange = ??? => dispatch(ChangeText(????));  
    <input value=state onChange/>
}

具体来说,punnedonChange处理程序的参数类型是什么,我如何对其进行解码?

我遇到的每一个参考都是为了JS,我很难翻译成Re

编辑 我通过抓取 github 找到的答案:

let onChange = event => dispatch(ChangeName(ReactEvent.Form.target(event)##value));

假设我想使用另一个 JSX 元素,文档在哪里?或者,他们是否假设从其他地方来到这里的人有先验知识?(我对'c'很满意)。

4

1 回答 1

0

您可以从https://github.com/rescript-lang/rescript-react/blob/v0.10.1/src/ReactDOM.res获取所有 DOM 属性的类型

该文件包含对 ReScript-React 的 DOM 属性子集的绑定。它有:

onChange: ReactEvent.Form.t => unit

ReactEvent.Form模块在https://github.com/rescript-lang/rescript-react/blob/v0.10.1/src/ReactEvent.resi#L168声明

当寻找任何特定于 ReScript-React 的东西时,搜索那个 repo。

看起来您现在有正确的代码来处理该事件。顺便说一句,您在某些地方有变体构造函数ChangeName,而在其他地方ChangeText,我认为正确的构造函数就是其中之一。编译器当然也会捕捉到这一点:-)

于 2021-03-13T05:20:07.803 回答