X Tutup
Skip to content

Commit 1e807af

Browse files
committed
Created an HOC to wrap components with ThemeProvider
1 parent 62c8284 commit 1e807af

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

src/components/Title.jsx

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import React from 'react';
2-
import PropTypes from 'prop-types';
3-
import styled, { ThemeProvider } from 'styled-components';
4-
import { connect } from 'react-redux';
5-
import { themeLight } from '../styles/themes/theme.light';
6-
import { themeDark } from '../styles/themes/theme.dark';
2+
import styled from 'styled-components';
3+
import withThemeProvider from '../hoc/withThemeProvider';
74

85
const StyledTitle = styled.h1`
96
font-family: 'Karla', sans-serif;
@@ -15,26 +12,6 @@ const StyledTitle = styled.h1`
1512
margin: 0;
1613
`;
1714

18-
export const Title = props => {
19-
const { text, mode } = props;
15+
export const Title = () => <StyledTitle>JavaScript Design Patterns</StyledTitle>;
2016

21-
return (
22-
<ThemeProvider theme={mode === 'dark' ? themeDark : themeLight}>
23-
<StyledTitle>{text}</StyledTitle>
24-
</ThemeProvider>
25-
);
26-
};
27-
28-
Title.propTypes = {
29-
text: PropTypes.string.isRequired,
30-
mode: PropTypes.string.isRequired
31-
};
32-
33-
Title.defaultProps = {
34-
text: 'JavaScript Design Patterns',
35-
mode: 'dark'
36-
};
37-
38-
const mapStateToProps = ({ mode }) => ({ mode });
39-
40-
export default connect(mapStateToProps)(Title);
17+
export default withThemeProvider(Title);

src/hoc/withThemeProvider.jsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { compose } from 'redux';
4+
import { connect } from 'react-redux';
5+
import { ThemeProvider } from 'styled-components';
6+
import { themeLight } from '../styles/themes/theme.light';
7+
import { themeDark } from '../styles/themes/theme.dark';
8+
9+
const withThemeProvider = Component => {
10+
const Sub = ({ mode }) => (
11+
<ThemeProvider theme={mode === 'dark' ? themeDark : themeLight}>
12+
<Component />
13+
</ThemeProvider>
14+
);
15+
16+
Sub.propTypes = {
17+
mode: PropTypes.string.isRequired
18+
};
19+
20+
return Sub;
21+
};
22+
23+
const mapStateToProps = ({ mode }) => ({ mode });
24+
25+
const composedWrapper = compose(
26+
connect(
27+
mapStateToProps,
28+
null
29+
),
30+
withThemeProvider
31+
);
32+
33+
export default composedWrapper;

0 commit comments

Comments
 (0)
X Tutup