我很惊讶这很难做到.但是我想出了这个,这似乎至少对我的简单案例起作用.有谁能推荐更好的方法?
def field_changed(self, fieldname):
"""Tests if the value of the field changed from the original data"""
orig_value = self.fields[fieldname].initial or getattr(self.instance, field, None)
orig_value = getattr(orig_value, 'pk', orig_value)
if type(orig_value) is bool:
# because None and False can be interchangeable
return bool(self.data.get(fieldname)) != bool(orig_value)
else:
return unicode(self.data.get(fieldname)) != unicode(orig_value)
Run Code Online (Sandbox Code Playgroud) 这是一个组件:
export default class MyComponent extends React.Component {
componentWillReceiveProps(newProps) {
console.log('RECEIVED PROPS');
}
render() {
return <div>{this.props.foo}</div>
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个包装器/更高阶组件:
const withSomething = (ComponentToWrap) => {
render() {
return <ComponentToWrap {...this.props} />
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个功能组件,它将MyComponent包装在withSomething中:
export default function WrappedComponent(props) {
const Component = withSomething(MyComponent);
return <Component ... some props ... />
}
Run Code Online (Sandbox Code Playgroud)
结果:即使我更新道具,MyComponent中与道具相关的生命周期函数(例如componentWillReceiveProps)也不会触发.
这怎么了?基于道具的生命周期方法不适用于包装组件吗?