Skip to content

Commit 9fdec4b

Browse files
Merge pull request #26 from klarna/stack-list-should-keep-context-and-context-component
Fix a bug with Context Parent component changing all the time, causing RemoteFrames to be remounted.
2 parents 02c13ff + 3050971 commit 9fdec4b

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "@klarna/remote-frames",
33
"version": "0.3.7",
44
"description": "Render a subset of the React tree to a different location, from many locations, without having to coordinate them",
5-
"main": "build/esm/index.js",
5+
"main": "src/index.js",
6+
"module": "build/esm/index.js",
67
"scripts": {
78
"clean": "rimraf -- build",
89
"build:esm": "cross-env BABEL_ENV=esm babel src/ --copy-files --out-dir build/esm/ --ignore spec.js,example/",

src/GlobalTarget.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,30 @@ class GlobalTarget extends Component {
2828

2929
this.stackTypes = []
3030

31-
this.SetContextComponent = createSetContextComponent({})
32-
3331
this.state = {
3432
stack: [],
3533
}
3634
}
3735

3836
componentDidMount() {
3937
this.renderInRemote = ({ jsx, context, contextTypes }) => {
40-
this.SetContextComponent = createSetContextComponent(contextTypes)
38+
const newStackItem = {
39+
jsx,
40+
SetContextComponent: createSetContextComponent(contextTypes),
41+
context
42+
};
4143

4244
if (this.stackTypes.filter(type => type === jsx.type).length === 0) {
4345
this.stackTypes = [...this.stackTypes, jsx.type]
4446

4547
this.setState(({ stack }) => ({
46-
context,
47-
stack: [...stack, jsx],
48+
stack: [...stack, newStackItem],
4849
}))
4950

5051
this.props.onAddStackElement(jsx)
5152
} else {
5253
this.setState(({ stack }) => ({
53-
context,
54-
stack: stack.map(item => (item.type === jsx.type ? jsx : item)),
54+
stack: stack.map(item => (item.jsx.type === jsx.type ? newStackItem : item)),
5555
}))
5656
}
5757
}
@@ -61,7 +61,7 @@ class GlobalTarget extends Component {
6161

6262
this.setState(
6363
({ stack }) => ({
64-
stack: stack.filter(({ type }) => type !== jsx.type),
64+
stack: stack.filter(item => item.jsx.type !== jsx.type),
6565
}),
6666
() => {
6767
this.props.onRemoveStackElement(jsx)
@@ -77,21 +77,20 @@ class GlobalTarget extends Component {
7777
}
7878

7979
render() {
80-
const { context, stack } = this.state
80+
const { stack } = this.state
8181

8282
return (
83-
<this.SetContextComponent context={context}>
84-
{stack.map((jsx, index) => {
83+
<React.Fragment>
84+
{stack.map(({jsx, SetContextComponent, context}, index) => {
8585
return (
86-
<div
87-
key={index}
88-
style={{ display: index === stack.length - 1 ? 'block' : 'none' }}
89-
>
90-
{jsx}
91-
</div>
86+
<SetContextComponent key={index} context={context}>
87+
<div style={{ display: index === stack.length - 1 ? 'block' : 'none' }}>
88+
{jsx}
89+
</div>
90+
</SetContextComponent>
9291
)
9392
})}
94-
</this.SetContextComponent>
93+
</React.Fragment>
9594
)
9695
}
9796
}

0 commit comments

Comments
 (0)