You can use reinspect, if you only want to use it during development then override the webpack resolve using rescripts
To do this you need to add the dependencies:
yarn add @rescripts/cli -D
yarn add @rescripts/rescript-env -D
yarn add reinspect -D
Add a file called .rescriptsrc.js
to the root of your project with the following content:
module.exports = [require.resolve("./.webpack.js")];
Then add a file called .webpack.js
to the root of your project with the following content:
const path = require("path");
module.exports = (config) => {
const file =
process.env.NODE_ENV === `development`
? "src/dev/useReducer.js"
: "src/prod/useReducer.js";
config.resolve.alias.useReducer = path.resolve(
__dirname,
file
);
//do the same with your root component (./index.js)
return config;
};
The dev/useReducer
is:
export { useReducer as default } from "reinspect";
The prod/useReducer
is:
export { useReducer as default } from "react";
In your component(s) you can do the following:
import useReducer from "useReducer";
...
const [state, dispatch] = useReducer(
reducer,
{
count: 0,
},
undefined,
"componentName"
);
The reason you need a dev and production component is because in production you don't want StateInspector, your root component should look something like this for development:
import { StateInspector } from "reinspect";
ReactDOM.render(
<StateInspector name="App">
<React.StrictMode>
<App />
</React.StrictMode>
</StateInspector>,
document.getElementById("root")
);
And like this for production:
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root")
);
Do not put StateInspector inside React.StrictMode because there seems to be a bug