1
我希望添加检查完成(一旦组件安装在CDM中)来检测userAgent - 为移动/闪光/触摸设备检测的目的而不是状态。这可能吗?如果是的话,你会怎么做?当我尝试访问isFlashInstalled
的上下文值时,我正在获取undefined
。这里是窥探组件设置背景:组件在React中装入后可以设置上下文吗?
App.js
export class App extends Component {
static childContextTypes = {
isFlashInstalled: React.PropTypes.bool
};
constructor() {
super();
this.state = {
isFlashInstalled: false
};
}
getChildContext() {
return {
isFlashInstalled: this.state.isFlashInstalled
};
}
componentDidMount() {
const flashVersion = require('../../../client/utils/detectFlash')();
// I know this could be done cleaner, focusing on how for now.
if (flashVersion && flashVersion.major !== 0) {
this.setFlashInstalled(true);
} else {
this.setFlashInstalled(false);
}
}
setFlashInstalled(status) {
this.setState({isFlashInstalled: status});
}
}
后来试图访问从上下文isFlashInstalled时,我会得到undefined
ChildComponent.js
export class ChildComponent extends Component {
// all the good stuff before render
render() {
const {isFlashInstalled} = this.context
console.log(isFlashInstalled); // undefined
}
}
我仍然无法使其为我,但因为你的答案显然是正确的,我要纪念工作它作为答案谢谢 –
谢谢你,希望你会成功 –