Created
April 28, 2020 17:46
-
-
Save doasync/f6f2c45d3526fc7ef08a6072e9101970 to your computer and use it in GitHub Desktop.
Effector modal 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
// Публичный API модалки | |
export { NameModal } from "./NameModal"; | |
export { openModal as openNameModal, nameIsUpdated } from "./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
// Для модалки лучше создать собственную модель. К примеру у тебя модалка с инпутом name и кнопкой save: | |
import { createEvent, createStore, restore, sample } from "effector"; | |
// Public | |
export const nameIsUpdated = createEvent(); | |
// Modal | |
export const openModal = createEvent(); | |
export const closeModal = createEvent(); | |
export const $isOpenModal = createStore(false); | |
// Actions | |
export const saveNameAction = createEvent(); | |
// Name | |
export const updateName = createEvent(); | |
export const $name = restore(updateName, ""); | |
// Init | |
$isOpenModal.on(openModal, () => true); | |
$isOpenModal.on(closeModal, () => false); | |
sample({ | |
source: $name, | |
clock: saveNameAction, | |
target: nameIsUpdated, | |
}); |
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 onSubmit = () => { | |
closeModal(); | |
saveNameAction(); | |
}; | |
// Component | |
export const NameModal = () => ( | |
<Modal | |
isOpen={useStore($isOpenModal)} | |
title="Enter new name” | |
onClose={closeModal} | |
onCancel={closeModal} | |
cancelText="Cancel" | |
onSubmit={onSubmit} | |
submitText="Save” | |
> | |
<NameInput /> | |
</Modal> | |
); |
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
// в другой модели подключаешь эту модалку к какому-нибудь экшену и подписываешься на основной ивент: | |
// ... | |
sample({ | |
source: changeNameAction, | |
target: openNameModal, | |
}); | |
sample({ | |
source: nameIsUpdated, | |
target: changeNameFx, | |
}); | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment