Skip to content

Commit dc94afc

Browse files
committedJan 22, 2016
Merge pull request #4 from whatknight/nextprops
Use next props when component will receive props.
2 parents 3b94a36 + 0a9673f commit dc94afc

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed
 

‎dist/switcheroo.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/index.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default class Switcher extends Component {
3030
super(props);
3131

3232
var currentPath = this.getLocation();
33-
var switchElement = this.getSwitch(currentPath);
33+
var switchElement = this.getSwitch(currentPath, props);
3434
this.state = {
3535
visibleSwitch: switchElement
3636
};
@@ -50,8 +50,7 @@ export default class Switcher extends Component {
5050

5151
componentWillReceiveProps(nextProps) {
5252
var currentPath = this.getLocation();
53-
var switchElement = this.getSwitch(currentPath);
54-
53+
var switchElement = this.getSwitch(currentPath, nextProps);
5554
this.setState({
5655
visibleSwitch: switchElement
5756
});
@@ -78,21 +77,21 @@ export default class Switcher extends Component {
7877
}
7978
};
8079

81-
getSwitch = (path) => {
82-
var children = [].concat(this.props.children);
80+
getSwitch(path, props) {
81+
var children = [].concat(props.children);
8382
var consistentPath = removeTrailingSlash(path);
8483
return children.filter(child => {
8584
var childPaths = [].concat(child.props.path).map(childPath => {
86-
return `${removeTrailingSlash(this.props.basePath + childPath)}/?`;
85+
return `${removeTrailingSlash(props.basePath + childPath)}/?`;
8786
});
8887
var regex = new RegExp(`^${childPaths.join('|')}$`);
8988
return regex.test(consistentPath);
9089
})[0] || null;
91-
};
90+
}
9291

9392
handleRouteChange = (ev) => {
9493
var currentPath = this.getLocation();
95-
var switchElement = this.getSwitch(currentPath);
94+
var switchElement = this.getSwitch(currentPath, this.props);
9695

9796
if (typeof this.props.onChange === 'function') {
9897
this.props.onChange(!!switchElement, currentPath);

‎test/Switcher_test.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -97,42 +97,42 @@ describe('Switcher', function() {
9797
});
9898

9999
it('gets component with matching path', function() {
100-
var swtch = this.switcher.getSwitch('/another');
100+
var swtch = this.switcher.getSwitch('/another', this.switcher.props);
101101
assert.equal(swtch.props.children, 'Another');
102102
});
103103

104104
it('handles trailing /', function() {
105-
var swtch = this.switcher.getSwitch('/another/');
105+
var swtch = this.switcher.getSwitch('/another/', this.switcher.props);
106106
assert.equal(swtch.props.children, 'Another');
107107
});
108108

109109
it('returns null if there is no matching switch', function() {
110-
var swtch = this.switcher.getSwitch('/notHere');
110+
var swtch = this.switcher.getSwitch('/notHere', this.switcher.props);
111111
assert.isNull(swtch);
112112
});
113113

114114
it('gets first match if duplicate paths', function() {
115-
var swtch = this.switcher.getSwitch('/duplicate');
115+
var swtch = this.switcher.getSwitch('/duplicate', this.switcher.props);
116116
assert.equal(swtch.props.children, 'Dup 1');
117117
});
118118

119119
it('handles paths with wild cards', function() {
120-
var swtch = this.switcher.getSwitch('/wildCardPath/something');
121-
var swtch2 = this.switcher.getSwitch('/wildCardPath/something/more');
120+
var swtch = this.switcher.getSwitch('/wildCardPath/something', this.switcher.props);
121+
var swtch2 = this.switcher.getSwitch('/wildCardPath/something/more', this.switcher.props);
122122
assert.equal(swtch.props.children, 'Wild');
123123
assert.equal(swtch2.props.children, 'Wild');
124124
});
125125

126126
it('handles paths with dynamic segments', function() {
127-
var swtch = this.switcher.getSwitch('/path/abc123/more');
128-
var swtch2 = this.switcher.getSwitch('/path/somethingelse/more');
127+
var swtch = this.switcher.getSwitch('/path/abc123/more', this.switcher.props);
128+
var swtch2 = this.switcher.getSwitch('/path/somethingelse/more', this.switcher.props);
129129
assert.equal(swtch.props.children, 'Dynamic');
130130
assert.equal(swtch2.props.children, 'Dynamic');
131131
});
132132

133133
it('handles array of paths', function() {
134-
var swtch = this.switcher.getSwitch('/arr1');
135-
var swtch2 = this.switcher.getSwitch('/arr2/more');
134+
var swtch = this.switcher.getSwitch('/arr1', this.switcher.props);
135+
var swtch2 = this.switcher.getSwitch('/arr2/more', this.switcher.props);
136136
assert.equal(swtch.props.children, 'Array');
137137
assert.equal(swtch2.props.children, 'Array');
138138
});
@@ -156,7 +156,7 @@ describe('Switcher', function() {
156156
});
157157

158158
it('gets component with matching path', function() {
159-
var swtch = this.switcher.getSwitch('/base/another');
159+
var swtch = this.switcher.getSwitch('/base/another', this.switcher.props);
160160
assert.equal(swtch.props.children, 'Another');
161161
});
162162
});

0 commit comments

Comments
 (0)
Please sign in to comment.