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

Show useSelector result in React DevTools #1530

Merged
merged 1 commit into from Feb 27, 2020
Merged

Show useSelector result in React DevTools #1530

merged 1 commit into from Feb 27, 2020

Conversation

Finesse
Copy link
Contributor

@Finesse Finesse commented Feb 27, 2020

Resolves #1525

I've checked the performance impact by calling useSelector 10000 times while rendering 1 component. Durations during mounts and updates:

Before After
React prod, browser console is closed 80ms, 45ms 81ms, 45ms
React dev, React DevTools are opened 100ms, 55ms 100ms, 55ms

The times vary a lot on each run so I've concluded that there is no noticeable performance difference and left the useDebugValue to be called regardless.

The benchmark code
<!--<script src="https://cdn.jsdelivr.net/npm/react@16.13.0/umd/react.production.min.js"></script>-->
<!--<script src="https://cdn.jsdelivr.net/npm/react-dom@16.13.0/umd/react-dom.production.min.js"></script>-->
<script src="https://cdn.jsdelivr.net/npm/react@16.13.0/umd/react.development.js"></script>
<script src="https://cdn.jsdelivr.net/npm/react-dom@16.13.0/umd/react-dom.development.js"></script>
<script src="https://cdn.jsdelivr.net/npm/redux@4.0.5/dist/redux.min.js"></script>
<script src="./react-redux.js"></script>

<div id="app"></div>
<script>
	var hookCallCount = 10000;

	var store = Redux.createStore(function(state) {
		return state || { foo: 'bar' };
	});

	function selector(state) {
		return state.foo;
	}

	function Component() {
		for (var i = 0; i < hookCallCount; ++i) {
			ReactRedux.useSelector(selector);
		}

		return 'Ok';
	}

	function App() {
		return React.createElement(
			ReactRedux.Provider,
			{ store: store },
			React.createElement(Component),
		);
	}

	console.time('First render');
	ReactDOM.render(
		React.createElement(App),
		document.getElementById('app'),
	);
	console.timeEnd('First render');

	setTimeout(() => {
		console.time('Update');
		ReactDOM.render(
			React.createElement(App),
			document.getElementById('app'),
		);
		console.timeEnd('Update');
	}, 1000);
</script>

Please consider that useDebugValue gives no effect with a React production bundle.

I haven't found a way to test a useDebugValue call therefore I haven't added a test. I will add if I know how.

@netlify
Copy link

netlify bot commented Feb 27, 2020

Deploy preview for react-redux-docs ready!

Built with commit 746f9e3

https://deploy-preview-1530--react-redux-docs.netlify.com

selector,
equalityFn,
store,
contextSub
)

useDebugValue(selectedState)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be at the root of useSelector to make Redux DevTools show the value right besides the hook name

@timdorr
Copy link
Member

timdorr commented Feb 27, 2020

LGTM. Thanks!

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

Successfully merging this pull request may close these issues.

[Feature request] Leverage useDebugValue in useSelector
2 participants