ReasonReact 减速器中的操作类型错误。

3
我正在尝试创建一个简单的待办事项应用程序,这是一个输入组件,我需要一个reducer来更新输入状态。这段代码会抛出错误 - 这个模式匹配的是action类型的值,但是期望匹配unit => string类型的值。出现这种情况的原因不明,请问有人能帮忙吗?
type state = string;
type action = 
  | InputChange(string);

let component = ReasonReact.reducerComponent("Input");

let make = (~onSubmit, _children) => {
  ...component,
  initialState: () => "",
  reducer: action => 
    switch (action) {
    | InputChange(text) => ReasonReact.Update(text)
    },
  render: ({state: todo, send}) =>
    <input
      className="input"
      value=todo
      type_="text"
      placeholder="What do you want todo"
      onChange={e => send(ReactEvent.Form.target(e)##value)}
      onKeyDown={
        e =>
          if (ReactEvent.Keyboard.key(e) == "Enter") {
            onSubmit(todo);
            send(() => "");
          }
      }
    />,
};
1个回答

4

render中使用send时,你将其传递为() => "",这是一个类型为unit => string的函数,推断出了action的类型。应该是send(InputChange(""))

另外,你在reducer中缺少state参数。它应该是reducer: (action, state) => ...,或者是reducer: (action, _state) => ...以避免未被使用的警告,因为你没有使用它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接