Skip to main content

React example

Just three lines of code, as highlighted in the snippet below, are all it takes to integrate mockforme into a ReactJS app.

./src/index.js
import React, { useEffect } from "react";
import ReactDOM from "react-dom/client";

/*
* you just need to add 3 lines of code and your mock api setup is ready.
*/
import { mockforme } from "mockforme";

const TOKEN = "ACCESS_TOKEN"; // change ACCESS_TOKEN with your token
mockforme(TOKEN).run();

const MyComponent = () => {
useEffect(() => {
fetch("/my-api")
.then((res) => res.json())
.then((data) => {
console.log("mocked data", data);
});
}, []);

return (
<div>Welcome mockforme</div>
)
};

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<MyComponent />
);