Skip to content
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

Research if usage based tracking gives render performance improvements #1

Closed
avkonst opened this issue Jul 12, 2019 · 1 comment
Closed

Comments

@avkonst
Copy link
Owner

avkonst commented Jul 12, 2019

Example using https://github.com/dai-shi/react-tracked:

More details here:
https://codesandbox.io/s/react-tracked-example-trb02
and here:
dai-shi/react-tracked#1

/* eslint-env browser */

import React, { useEffect, useRef, StrictMode } from "react";
import ReactDOM from "react-dom";

import { Provider, useTracked } from "react-tracked";
import { useStateLink } from "react-use-state-x";

const initialState = {
  count1: 0,
  deep: {
    count2: 0
  }
};

const useValue = () => {
  const link = useStateLink(initialState);
  const linkRef = useRef(link);
  useEffect(() => {
    linkRef.current = link;
  });
  return [link.value, linkRef];
};

const Counter1 = () => {
  const [value, linkRef] = useTracked();
  const increment = () => {
    linkRef.current.nested.count1.set(c => c + 1);
  };
  return (
    <div>
      <span>Count1: {value.count1}</span>
      <button type="button" onClick={increment}>
        +1
      </button>
      {Math.random()}
    </div>
  );
};

const Counter2 = () => {
  const [value, linkRef] = useTracked();
  const increment = () => {
    linkRef.current.nested.deep.nested.count2.set(c => c + 1);
  };
  return (
    <div>
      <span>Count2: {value.deep.count2}</span>
      <button type="button" onClick={increment}>
        +1
      </button>
      {Math.random()}
    </div>
  );
};

const App = () => (
  <StrictMode>
    <Provider useValue={useValue}>
      <Counter1 />
      <Counter1 />
      <Counter2 />
      <Counter2 />
    </Provider>
  </StrictMode>
);

ReactDOM.unstable_createRoot(document.getElementById("app")).render(<App />);

@avkonst avkonst changed the title Research if usage based tracking can give render performance improvements Research if usage based tracking gives render performance improvements Jul 12, 2019
@avkonst
Copy link
Owner Author

avkonst commented Aug 15, 2019

It does. See the example: https://hookstate.netlify.com/performance-demo-large-table

@avkonst avkonst closed this as completed Aug 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant