-
-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to create an Elm like hook? #20
Comments
The reason it's rendering twice is because of the
then the hook would return an observable without subscribing to updates. And then rendering the observable directly in As for the concept, it looks like it would work. But it is a lot of extra code. I'm curious what the overall goal is - maybe we could come up with something easier/shorter that could achieve the same goal? |
That makes sense, thanks for explaining! I guess, I want something that comes close to useReducer. |
I added a export function useObservableReducer(reducer, initializerArg, initializer) {
const obs = useObservable(initializerArg !== undefined ? initializer(initializerArg) : initializerArg);
const dispatch = (action) => {
obs.set(reducer(obs.get(), action));
};
return [obs, dispatch];
} Does it seem correct to you? I don't use reducers so I'm not 100% sure of all the ways to use them. |
Many thanks! |
@jmeistrich any documentation for it? |
@attiqeurrehman Oh I forgot to add that to the docs! I'll add that to my todo list and reopen this issue until it is documented. But it should be pretty straightforward in theory - use it as you would use |
@jmeistrich thanks for the quick reply. I am having trouble with reducer actions: function tasksReducer(tasks, action) {
switch (action.type) {
case 'added': {
return [
...tasks,
{
id: action.id,
text: action.text,
done: false,
},
];
}
case 'changed': {
return tasks.map((t) => {
if (t.id === action.task.id) {
return action.task;
} else {
return t;
}
});
}
case 'deleted': {
return tasks.filter((t) => t.id !== action.id);
}
default: {
throw Error('Unknown action: ' + action.type);
}
}
}
const initialTasks = [
{id: 0, text: 'Visit Kafka Museum', done: true},
{id: 1, text: 'Watch a puppet show', done: false},
{id: 2, text: 'Lennon Wall pic', done: false},
];
const [tasks, dispatch] = useReducer(tasksReducer, initialTasks); how it will look with the observable? |
You should be able to just replace the const [observableTasks, dispatch] = useObservableReducer(tasksReducer, initialTasks);
const tasks = observableTasks.get() Is that not working? If not, if you can post a codesandbox or a full example with expected results for me to test with, that would be very helpful to track down the problem! |
Here is the codesandbox but I am getting the following error:
|
@attiqeurrehman Got it, thanks. I'll fix that soon and let you know. |
sounds good, looking forward to it. |
Fixed in |
@jmeistrich that was fast and awesome! |
Hello, I'm wondering how I could leverage this project to create an Elm style (or reducerlike) hook.
Something along the lines of:
The model type in this simple example is just a number and my reducer function
update
takes in a message and creates a new model based on the receivedmsg
.In this example, I get two new renders for each click, so I'm most likely missing the point somewhere.
Any thoughts on this?
The text was updated successfully, but these errors were encountered: