Skip to content

Commit 65317d0

Browse files
committed
Add Redux
1 parent a415b98 commit 65317d0

File tree

10 files changed

+59
-8
lines changed

10 files changed

+59
-8
lines changed

fixtures/nesting/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"react-scripts": "3.4.1"
6+
"react-scripts": "3.4.1",
7+
"redux": "^4.0.5"
78
},
89
"scripts": {
910
"postinstall": "run-p install:*",

fixtures/nesting/src/legacy/Greeting.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import React from 'react';
22
import {Component} from 'react';
33
import {findDOMNode} from 'react-dom';
44
import {Link} from 'react-router-dom';
5+
import {connect} from 'react-redux';
6+
57
import ThemeContext from './shared/ThemeContext';
68
import Clock from './shared/Clock';
79

8-
export default class AboutSection extends Component {
10+
class AboutSection extends Component {
911
static contextType = ThemeContext;
1012

1113
componentDidMount() {
@@ -23,10 +25,22 @@ export default class AboutSection extends Component {
2325
This component is rendered by the nested React.
2426
</h4>
2527
<Clock />
28+
<p>
29+
Counter: {this.props.counter}{' '}
30+
<button onClick={() => this.props.dispatch({type: 'increment'})}>
31+
+
32+
</button>
33+
</p>
2634
<b>
2735
<Link to="/">Go to Home</Link>
2836
</b>
2937
</div>
3038
);
3139
}
3240
}
41+
42+
function mapStateToProps(state) {
43+
return {counter: state};
44+
}
45+
46+
export default connect(mapStateToProps)(AboutSection);

fixtures/nesting/src/legacy/createLegacyRoot.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ import ThemeContext from './shared/ThemeContext';
77
// Note: this is a semi-private API, but it's ok to use it
88
// if we never inspect the values, and only pass them through.
99
import {__RouterContext} from 'react-router';
10+
import {ReactReduxContext} from 'react-redux';
1011

1112
// Pass through every context required by this tree.
1213
// The context object is populated in src/modern/withLegacyRoot.
1314
function Bridge({children, context}) {
1415
return (
1516
<ThemeContext.Provider value={context.theme}>
1617
<__RouterContext.Provider value={context.router}>
17-
{children}
18+
<ReactReduxContext.Provider value={context.reactRedux}>
19+
{children}
20+
</ReactReduxContext.Provider>
1821
</__RouterContext.Provider>
1922
</ThemeContext.Provider>
2023
);

fixtures/nesting/src/legacy/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"dependencies": {
55
"react": "0.0.0-3d0895557",
66
"react-dom": "0.0.0-3d0895557",
7+
"react-redux": "7.2.1",
78
"react-router-dom": "5.2.0"
89
}
910
}
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import React from 'react';
22
import {useContext} from 'react';
3+
import {connect} from 'react-redux';
34

45
import ThemeContext from './shared/ThemeContext';
56
import lazyLegacyRoot from './lazyLegacyRoot';
67

78
// Lazy-load a component from the bundle using legacy React.
89
const Greeting = lazyLegacyRoot(() => import('../legacy/Greeting'));
910

10-
export default function AboutPage() {
11+
function AboutPage({counter, dispatch}) {
1112
const theme = useContext(ThemeContext);
1213
return (
1314
<>
@@ -16,6 +17,17 @@ export default function AboutPage() {
1617
This component is rendered by the outer React.
1718
</h3>
1819
<Greeting />
20+
<br />
21+
<p>
22+
Counter: {counter}{' '}
23+
<button onClick={() => dispatch({type: 'increment'})}>+</button>
24+
</p>
1925
</>
2026
);
2127
}
28+
29+
function mapStateToProps(state) {
30+
return {counter: state};
31+
}
32+
33+
export default connect(mapStateToProps)(AboutPage);

fixtures/nesting/src/modern/HomePage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {Link} from 'react-router-dom';
55
import ThemeContext from './shared/ThemeContext';
66
import Clock from './shared/Clock';
77

8-
export default function HomePage() {
8+
export default function HomePage({counter, dispatch}) {
99
const theme = useContext(ThemeContext);
1010
return (
1111
<>

fixtures/nesting/src/modern/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import React from 'react';
22
import {StrictMode} from 'react';
33
import ReactDOM from 'react-dom';
4+
import {Provider} from 'react-redux';
45
import App from './App';
6+
import {store} from '../store';
57

68
ReactDOM.render(
79
<StrictMode>
8-
<App />
10+
<Provider store={store}>
11+
<App />
12+
</Provider>
913
</StrictMode>,
1014
document.getElementById('root')
1115
);

fixtures/nesting/src/modern/lazyLegacyRoot.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React from 'react';
22
import {useContext, useMemo, useRef, useState, useLayoutEffect} from 'react';
3-
43
import {__RouterContext} from 'react-router';
4+
import {ReactReduxContext} from 'react-redux';
5+
56
import ThemeContext from './shared/ThemeContext';
67

78
let rendererModule = {
@@ -29,12 +30,14 @@ export default function lazyLegacyRoot(getLegacyComponent) {
2930
// Then in src/legacy/createLegacyRoot we will apply them.
3031
const theme = useContext(ThemeContext);
3132
const router = useContext(__RouterContext);
33+
const reactRedux = useContext(ReactReduxContext);
3234
const context = useMemo(
3335
() => ({
3436
theme,
3537
router,
38+
reactRedux,
3639
}),
37-
[theme, router]
40+
[theme, router, reactRedux]
3841
);
3942

4043
// Create/unmount.

fixtures/nesting/src/modern/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"dependencies": {
55
"react": "0.0.0-3d0895557",
66
"react-dom": "0.0.0-3d0895557",
7+
"react-redux": "7.2.1",
78
"react-router-dom": "5.2.0"
89
}
910
}

fixtures/nesting/src/store.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {createStore} from 'redux';
2+
3+
function reducer(state = 0, action) {
4+
switch (action.type) {
5+
case 'increment':
6+
return state + 1;
7+
default:
8+
return state;
9+
}
10+
}
11+
12+
export const store = createStore(reducer);

0 commit comments

Comments
 (0)