Created
October 3, 2022 20:46
-
-
Save brandonchadlange/d3f1a004837bc4bf811e575186a3b6fe to your computer and use it in GitHub Desktop.
Example add todo view model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useAddTodoVm = (onAdd: (description: string) => void) => { | |
const [inputText, setInputText] = useState(""); | |
const addTodo = () => { | |
onAdd(inputText); | |
setInputText(""); | |
}; | |
return { | |
inputText, | |
setInputText, | |
addTodo, | |
}; | |
}; | |
interface AddTodoViewModel { | |
inputText: string; | |
setInputText: (inputText: string) => void; | |
addTodo: () => void; | |
} | |
const AddTodo = ({ viewModel }: { viewModel: AddTodoViewModel }) => { | |
return ( | |
<> | |
<input | |
value={viewModel.inputText} | |
onChange={(e) => viewModel.setInputText(e.target.value)} | |
type="text" | |
placeholder="Add todo" | |
/> | |
<button onClick={viewModel.addTodo}>Add</button> | |
</> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment