Skip to content

Commit

Permalink
Merge pull request #25 from 14-team13/13-feature-state-management-recoil
Browse files Browse the repository at this point in the history
13 feature state management recoil
  • Loading branch information
nickkies authored Mar 15, 2023
2 parents 0fc1a2a + 398e18e commit 36bf8a9
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 20 deletions.
7 changes: 6 additions & 1 deletion host/config-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ module.exports = function (config, env) {
config.plugins.push(
new ModuleFederationPlugin(
(module.exports = {
name: "remote",
name: "host",
remotes: {
remote: `remote@http://localhost:3001/remoteEntry.js`,
},
exposes: {
"./atoms": "./src/store/atoms",
},
filename: "remoteEntry.js",
shared: {
...dependencies,
react: {
Expand All @@ -21,6 +25,7 @@ module.exports = function (config, env) {
singleton: true,
requiredVersion: dependencies["react-dom"],
},

},
})
),
Expand Down
1 change: 1 addition & 0 deletions host/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"react-dom": "^18.2.0",
"react-router-dom": "^6.7.0",
"react-scripts": "5.0.1",
"recoil": "^0.7.7",
"run-script-os": "^1.1.6",
"swr": "^2.0.4",
"typescript": "^4.9.4",
Expand Down
17 changes: 9 additions & 8 deletions host/src/bootstrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import App from "./App";
import reportWebVitals from "./reportWebVitals";
import "./index.css";
import { BrowserRouter } from "react-router-dom";
import axios from "axios";
axios.defaults.baseURL = "http://localhost:8123"; // 임시 서버
axios.defaults.withCredentials = true;
import { RecoilRoot } from 'recoil';


const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
);

root.render(
<BrowserRouter>
<React.StrictMode>
<App />
</React.StrictMode>
</BrowserRouter>
<RecoilRoot>
<BrowserRouter>
<React.StrictMode>
<App />
</React.StrictMode>
</BrowserRouter>
</RecoilRoot>
);

// If you want to start measuring performance in your app, pass a function
Expand Down
12 changes: 12 additions & 0 deletions host/src/store/atoms.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { atom, RecoilEnv } from "recoil";
RecoilEnv.RECOIL_DUPLICATE_ATOM_KEY_CHECKING_ENABLED = false;

export const countState = atom({
key: "countState",
default: 0
});

export const userState = atom({
key: 'user',
default: { email: '[email protected]', username: 'aluminum' }
});
3 changes: 3 additions & 0 deletions remote/config-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = function (config, env) {
new ModuleFederationPlugin(
(module.exports = {
name: "remote",
remotes: {
host: `host@http://localhost:3000/remoteEntry.js`,
},
exposes: {
"./Menu": "./src/components/Menu"
},
Expand Down
1 change: 1 addition & 0 deletions remote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"react-dom": "^18.2.0",
"react-error-boundary": "^3.1.4",
"react-scripts": "5.0.1",
"recoil": "^0.7.7",
"styled-components": "^5.3.8"
},
"scripts": {
Expand Down
14 changes: 9 additions & 5 deletions remote/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { RecoilRoot } from 'recoil';

function App() {
return (
<div className="App">
<header className="App-header" data-e2e="REMOTE_COMPONENT_INFO">
Remote Application
</header>
</div>
<RecoilRoot>
<div className="App">
<header className="App-header" data-e2e="REMOTE_COMPONENT_INFO">
Remote Application
</header>
</div>
</RecoilRoot>
);
}

Expand Down
30 changes: 30 additions & 0 deletions remote/src/components/Maps/LeftModal.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from "react";
import { useRecoilState, useResetRecoilState, useRecoilValue } from "recoil";
import { userState } from "host/atoms";
import { countState } from "host/atoms";

export const Counter = () => {
const [count, setCount] = useRecoilState(countState);
const user = useRecoilValue(userState);
const resetCount = useResetRecoilState(countState);

const increase = () => {
setCount(count + 1);
};

const reset = () => {
resetCount();
};

return (
<div>
<div>
<div>{user.email}</div>
<div>{user.username}</div>
</div>
<h2>{count}</h2>
<button onClick={() => increase()}>+</button>
<button onClick={() => reset()}>reset</button>
</div>
);
};
15 changes: 9 additions & 6 deletions remote/src/components/Menu.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import styled from 'styled-components';

import Maps from 'components/Maps/Maps';
import { MixedBoundary } from 'components/Common';
import { Counter } from './Maps/LeftModal';

export const Menu = () => {
const Div = styled.div`
Expand All @@ -10,11 +10,14 @@ export const Menu = () => {
`;
// let MapsComponent = React.lazy(() => import('./components/Maps'));
return (
<Div>
<MixedBoundary>
<Maps />
</MixedBoundary>
</Div>
<div style = {{display : "flex"}}>
<Counter/>
<Div>
<MixedBoundary>
<Maps />
</MixedBoundary>
</Div>
</div>
);
};
export default Menu;

0 comments on commit 36bf8a9

Please sign in to comment.