ReasonReact reducer 中的动作类型错误

action has wrong type in ReasonReact reducer

我正在尝试创建一个简单的待办事项应用程序,这是一个输入组件,我需要一个 reducer 来更新输入的状态。此代码抛出错误 - This pattern matches values of type action but a pattern was expected which matches values of type unit => string

出于某种原因,它期望 actionunit => 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(() => "");
          }
      }
    />,
};

action 的类型是通过在 render 中使用 send 推断出来的,你在其中传递它 () => "",类型为 unit => string 的函数.应该是 send(InputChange("")).

您还缺少 reducer 上的 state 参数。它应该是 reducer: (action, state) => ...reducer: (action, _state) => ... 以避免未使用的警告,因为您没有使用它。