arthurclemens / mithril-hooks Goto Github PK
View Code? Open in Web Editor NEWUse hooks in Mithril
Use hooks in Mithril
Hi @ArthurClemens, first of all, I love what you did with polythene, awesome!
We've implemented polythene-mithril
into our project and it works like a charm :-) The only issue is running tests with jest/mithril-query. Our testing is setup with jest
, jsdom
and [email protected]
and we get the following error when running our tests since adding polythene-mithril
:
$ jest --detectOpenHandles
FAIL tests/happy.test.ls (5.155 s)
✕ view_dashboard (1962 ms)
● view_dashboard
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Document'
--- property '_previousSibling' closes the circle
at JSON.stringify (<anonymous>)
30 | view_dashboard = require '/src/views/startblock/dashboarding/view_dashboard'
> 31 | out = mq view_dashboard, actions: actions
| ^
32 |
33 | # Check if there are loader components active
at instanceClass (node_modules/cyano-mithril/node_modules/mithril-hooks/dist/mithril-hooks.mjs:105:40)
at deps (node_modules/polythene-core-base-spinner/src/index.js:60:15)
at Object.oncreate (node_modules/cyano-mithril/node_modules/mithril-hooks/dist/mithril-hooks.mjs:114:65)
at Function.callHook (node_modules/mithril/render/render.js:30:16)
at node_modules/mithril/render/render.js:971:50
at redraw (node_modules/mithril-query/index.js:246:24)
at init (node_modules/mithril-query/index.js:248:3)
at Object.<anonymous> (tests/happy.test.ls:31:11)
I tried to exclude the spinner, and then it threw the same error exception for polythene-core-textfield
in stead of polythene-core-base-spinner
.
It's probably an incompatibility between mithril-query
and mithril-hooks
and I was wondering if you might be able to help understanding what the issue is. The exception seems to occur on line https://github.com/ArthurClemens/mithril-hooks/blob/master/packages/mithril-hooks/src/index.ts#L144 in your source.
I'm quite clueless in how to resolve this issue, so any help would be appreciated!
React hooks will call the teardown function each time the effect is restarted to perform any necessary cleanup. Mithril hooks does not do this atm.
As you change the values in the input new event listeners are added, but never removed (until component unmounts).
I'm not sure if much can be done about this, and it might have more to do with how dom refs are handled than hooks themselves (ref
vs oncreate
), but I figured I'd file it to see what you think.
I have 3 example of a custom hook that I wrote, worked well in preact/react, but not when I tried converting to mithril. My current thought is that it has to do with a different in priority for useState
updates during a redraw.. but I haven't dug too deep.
The issue is that after calling setElem
from the oncreate
lifecycle hook in mithril, the useLayoutEffect
hook is not called quickly enough to allow me to animate the element.
I managed to get it basically working for mithril, by calling the onenter function from the setter rather than waiting for the useLayoutEffect
hook to fire again.
Feel free to close this if it seems out of scope/irrelevant.
Hi,
I am currently working with Mithril.js and I came across an interesting feature in React that I would like to replicate. In React, we have hooks, such as useState and useEffect, which provide a convenient way to manage state and perform side effects within a functional component.
I have a specific use case where I want to replicate the functionality of the useDetails hook from React. The code snippet below demonstrates how it is used in React:
<State>
{([]) => {
const { getDetailsProps, open, setOpen } = useDetails({ closeOnOutsideClick: true });
const { onToggle, ...detailsProps } = getDetailsProps();
const customToggle = e => {
onToggle(e);
window.alert('hi');
};
return (
<Details {...detailsProps} onToggle={customToggle}>
<Button as="summary">{open ? 'Open' : 'Closed'}</Button>
Hello World
</Details>
);
}}
</State>
This code utilizes the useDetails hook to handle the state and functionality of a Details component. The open state value determines whether the details are open or closed, and the onToggle function is called when the details are toggled. Additionally, an alert is shown with the message “hi” when the toggle happens.
I would like to achieve a similar functionality in Mithril.js but I am not sure how to approach it. Could you please provide guidance on how I can implement a custom hook in Mithril.js to handle this use case?
Thank you in advance for your assistance!
Hi!
First of all, this library is an excellent idea and I love it so far.
I found an issue on Storybook that could be a another good idea to fix.
I looked at your code but I didn't found where it failed 😄
With a simple "Storybook for Mithril" and a simple story and component, it failed.
File stories/hooks.stories.js
import m from 'mithril';
import {
storiesOf
} from '@storybook/mithril';
import HookTest from "../components/test.js";
storiesOf('HookTest', module)
.add('basic process', () => {
return {
view: () => m(HookTest)
};
})
File: components/test.js
import m from "mithril";
import {
withHooks,
useEffect,
useState,
} from "mithril-hooks";
export default withHooks(function() {
const [ count, set ] = useState(0);
useEffect(() => {
console.log(count);
set(count + 1);
}, []);
return m('div', "hello");
});
In the result, you should have the console.log(count);
showing 0
indefinitely in the console of the browser 😄
I hope you'll figure it out better than me on your library.
Thx a lot for your work!
TypeError: Cannot read properties of undefined (reading 'statesIndex')
at updateState (MithrilHooks.ts:80:23)
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates are currently rate-limited. Click on a checkbox below to force their creation now.
@typescript-eslint/eslint-plugin
, @typescript-eslint/parser
)@typescript-eslint/eslint-plugin
, @typescript-eslint/parser
)These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
.github/workflows/main.yml
actions/checkout v3
actions/setup-node v3
actions/cache v3
actions/checkout v3
actions/setup-node v3
actions/cache v3
ubuntu 20.04
ubuntu 20.04
packages/test-mithril-hooks/index.html
bulma 0.9.4
package.json
@typescript-eslint/eslint-plugin ^6.0.0
@typescript-eslint/parser ^6.0.0
cypress ^12.7.0
eslint ^8.34.0
eslint-config-airbnb ^19.0.4
eslint-config-next ^13.2.1
eslint-config-prettier 8.10.0
eslint-import-resolver-typescript 3.6.1
eslint-plugin-cypress ^2.12.1
eslint-plugin-import 2.29.1
eslint-plugin-jest-dom 5.1.0
eslint-plugin-json 3.1.0
eslint-plugin-jsx-a11y ^6.7.1
eslint-plugin-prettier 5.1.3
eslint-plugin-simple-import-sort 10.0.0
npm-check-updates ^16.7.9
npm-run-all ^4.1.5
postcss-cli ^10.1.0
prettier ^3.0.0
react ^18.2.0
rollup-plugin-filesize ^10.0.0
typescript ^5.0.0
vite ^4.1.4
packages/mithril-hooks/package.json
flatted ^3.2.7
@types/mithril ^2.0.12
mithril ^2.0.4
packages/test-mithril-hooks/package.json
mithril 2.2.2
@types/mithril 2.0.12
start-server-and-test ^2.0.0
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.