3

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

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

1 回答 1

4

的类型action是通过使用sendin来推断的render,您在其中传递() => ""了 type 的函数unit => string。应该是send(InputChange(""))

你也错过了state关于reducer. 它应该是reducer: (action, state) => ...,或者reducer: (action, _state) => ...避免未使用的警告,因为您没有使用它。

于 2018-12-06T02:10:43.963 回答