【问题标题】:Where is the documentation to write an event handler for input text box?为输入文本框编写事件处理程序的文档在哪里?
【发布时间】:2021-05-14 05:45:12
【问题描述】:

本来我想知道的:

如何为此编写处理程序?

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/>
}

具体来说,双关语onChange 处理程序的参数类型是什么,如何解码?

我遇到的每个参考都是针对JS,我很难将其翻译成Re

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

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

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

【问题讨论】:

    标签: reason rescript


    【解决方案1】:

    你可以从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,我认为正确的构造函数就是其中之一。编译器当然也会捕捉到这一点:-)

    【讨论】:

    • 这是一个匆忙输入的例子:ChangeName/Text 本来就是同一个变体。感谢您的文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 2013-08-04
    相关资源
    最近更新 更多