Skip to content

Commit

Permalink
performance improve!
Browse files Browse the repository at this point in the history
  • Loading branch information
apple authored and apple committed Aug 22, 2020
1 parent a020e02 commit df1e6e7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-one",
"version": "0.4.0",
"version": "0.5.0",
"license": "MIT",
"author": "suhaotian",
"main": "dist/index.js",
Expand Down
10 changes: 5 additions & 5 deletions src/createOne.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect } from 'react';
import { useState, useEffect, useRef } from 'react';
import eventemitter3 from 'eventemitter3';

let ID = 0;
Expand Down Expand Up @@ -39,18 +39,18 @@ export function createOne<T>(
Readonly<T> | (() => Readonly<T>),
(newValue: Readonly<T> | (() => Readonly<T>)) => void
] {
const [updateCount, setUpdateCount] = useState(0);
const updateCountRef = useRef(0);
const [, setUpdateCount] = useState(0);

useEffect(() => {
function updater() {
setUpdateCount(updateCount + 1);
setUpdateCount(++updateCountRef.current);
}
// create and destroy at every render? currently yes...
eventBus.on(EVENT_NAME, updater);
return () => {
eventBus.off(EVENT_NAME, updater);
};
}, [updateCount]);
}, []);

return [_state, setState];
}
Expand Down

0 comments on commit df1e6e7

Please sign in to comment.