git.fiddlerwoaroof.com
Browse code

Fixing optional react dependency

Max Summe authored on 19/06/2017 22:57:51
Showing 1 changed files
... ...
@@ -1,12 +1,14 @@
1
-try { // making this optional dependency b/c it's automatically exported but only
2
-     // useful when react is installed
3
-  const React = require('react');
4
-} catch(e) {
5
-}
1
+// Ugly way to deal with optional dependency so we don't break projects not using react.
2
+let React = null;
3
+
4
+const ActionLink = ({action, children}, {store}) => {
6 5
 
7
-export default function ActionLink({action, children}, {store}) {
8 6
 
9
-  if(!store) {
7
+  if (!React) {
8
+    throw new Error("You cannot use ActionLink unless react is available");
9
+  }
10
+
11
+  if (!store) {
10 12
     throw new Error("You cannot use ActionLink without providing store via context (possibly using react-redux Provider?)");
11 13
   }
12 14
 
... ...
@@ -15,10 +17,20 @@ export default function ActionLink({action, children}, {store}) {
15 17
   return (
16 18
     <a href={renderedRoute}
17 19
        onClick={ev => {
18
-        ev.preventDefault();
19
-        store.dispatch(action);}}>{children}</a>
20
+         ev.preventDefault();
21
+         store.dispatch(action);
22
+       }}>{children}</a>
20 23
   );
21 24
 };
22
-ActionLink.contextTypes = {
23
-  store: React.PropTypes.object
24
-};
25
+
26
+try {
27
+  React = require('react');
28
+  ActionLink.contextTypes = {
29
+    store: React.PropTypes.object
30
+  };
31
+
32
+} catch (e) {
33
+
34
+}
35
+
36
+export default ActionLink;