Skip to content

Commit f8657bc

Browse files
zgotschjaredly
authored andcommitted
Add a way to forceUpdate stateless components (facebook#911)
* Add a way to forceUpdate stateless components, so dev tools can change their props + context * guard forceUpdate function
1 parent e253c66 commit f8657bc

2 files changed

Lines changed: 26 additions & 23 deletions

File tree

backend/getData.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,17 @@ function getData(internalInstance: Object): DataType {
127127

128128
if (internalInstance._instance) {
129129
var inst = internalInstance._instance;
130+
131+
// A forceUpdate for stateless (functional) components.
132+
var forceUpdate = inst.forceUpdate || (inst.updater && inst.updater.enqueueForceUpdate && function(cb) {
133+
inst.updater.enqueueForceUpdate(this, cb, 'forceUpdate');
134+
});
130135
updater = {
131136
setState: inst.setState && inst.setState.bind(inst),
132-
forceUpdate: inst.forceUpdate && inst.forceUpdate.bind(inst),
133-
setInProps: inst.forceUpdate && setInProps.bind(null, internalInstance),
137+
forceUpdate: forceUpdate && forceUpdate.bind(inst),
138+
setInProps: forceUpdate && setInProps.bind(null, internalInstance, forceUpdate),
134139
setInState: inst.forceUpdate && setInState.bind(null, inst),
135-
setInContext: inst.forceUpdate && setInContext.bind(null, inst),
140+
setInContext: forceUpdate && setInContext.bind(null, inst, forceUpdate),
136141
};
137142
if (typeof type === 'function') {
138143
publicInstance = inst;
@@ -172,23 +177,23 @@ function getData(internalInstance: Object): DataType {
172177
};
173178
}
174179

175-
function setInProps(internalInst, path: Array<string | number>, value: any) {
180+
function setInProps(internalInst, forceUpdate, path: Array<string | number>, value: any) {
176181
var element = internalInst._currentElement;
177182
internalInst._currentElement = {
178183
...element,
179184
props: copyWithSet(element.props, path, value),
180185
};
181-
internalInst._instance.forceUpdate();
186+
forceUpdate.call(internalInst._instance);
182187
}
183188

184189
function setInState(inst, path: Array<string | number>, value: any) {
185190
setIn(inst.state, path, value);
186191
inst.forceUpdate();
187192
}
188193

189-
function setInContext(inst, path: Array<string | number>, value: any) {
194+
function setInContext(inst, forceUpdate, path: Array<string | number>, value: any) {
190195
setIn(inst.context, path, value);
191-
inst.forceUpdate();
196+
forceUpdate.call(inst);
192197
}
193198

194199
function setIn(obj: Object, path: Array<string | number>, value: any) {

test/example/target.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,22 +198,20 @@ class HoverHighlight extends React.Component {
198198
}
199199
}
200200

201-
class Filter extends React.Component {
202-
render() {
203-
var options = ['All', 'Completed', 'Remaining'];
204-
return (
205-
<div style={styles.filter}>
206-
{options.map(text => (
207-
<button
208-
key={text}
209-
style={assign({}, styles.filterButton, text === this.props.filter && styles.filterButtonActive)}
210-
onClick={this.props.onFilter.bind(null, text)}
211-
>{text}</button>
212-
))}
213-
{/*<button onClick={this.props.onSort} style={styles.filterButton}>Sort</button>*/}
214-
</div>
215-
);
216-
}
201+
function Filter(props) {
202+
var options = ['All', 'Completed', 'Remaining'];
203+
return (
204+
<div style={styles.filter}>
205+
{options.map(text => (
206+
<button
207+
key={text}
208+
style={assign({}, styles.filterButton, text === props.filter && styles.filterButtonActive)}
209+
onClick={props.onFilter.bind(null, text)}
210+
>{text}</button>
211+
))}
212+
{/*<button onClick={this.props.onSort} style={styles.filterButton}>Sort</button>*/}
213+
</div>
214+
);
217215
}
218216

219217
var styles = {

0 commit comments

Comments
 (0)