admin管理员组文章数量:1296844
In my main ponent <App />
of my meteorJS application I'm using some user data (const user = Meteor.user()
).
If the user
-object has a check
value <AnotherComponent />
will be used. There the user can do some interaction and the check
field gets removed from user
document.
To make it a bit clearer: The check
value will be removed on another place by updating the value in the db.
Now I would expect, that now <MainComponent />
will be rendered. But it won't. I need to reload the page to get it.
Is there a way how I can make the user
variable 'reactive'?
class App extends Component {
render () {
const user = Meteor.user()
if (user && user.check) {
return (<AnotherComponent />)
}
return (<MainComponent />)
}
}
In my main ponent <App />
of my meteorJS application I'm using some user data (const user = Meteor.user()
).
If the user
-object has a check
value <AnotherComponent />
will be used. There the user can do some interaction and the check
field gets removed from user
document.
To make it a bit clearer: The check
value will be removed on another place by updating the value in the db.
Now I would expect, that now <MainComponent />
will be rendered. But it won't. I need to reload the page to get it.
Is there a way how I can make the user
variable 'reactive'?
class App extends Component {
render () {
const user = Meteor.user()
if (user && user.check) {
return (<AnotherComponent />)
}
return (<MainComponent />)
}
}
Share
Improve this question
asked Aug 10, 2017 at 20:21
user3142695user3142695
17.4k55 gold badges194 silver badges375 bronze badges
2 Answers
Reset to default 4if you want to reload a render, your ponent has need a variable in a state that can be change. Or receive some new props.
In your case, this is normal you've to reload the page.
if you want to run render() in App ponent, you need to store the user in a state, and find a way to call Meteor.user again to retrieve some new data and replace in the user state. If the state.user has changed, then, your ponent will be rerendering.
class App extends Component {
constructor() {
super();
this.state = {
user: Meteor.user(),
}
this.smthChanging = this.smthChanging.bind(this);
}
smthChanging() {
this.setState({
user: Meteor.user(),
});
}
render () {
const { user } = this.state;
if (user && user.check) {
return (<AnotherComponent />)
}
return (
<div>
<button
onClick={this.smthChanging}
>
reload user
</button>
</div>
)
}
}
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>
Try moving the user declaration into state. That way, when Meteor.user() updates, it will update state and trigger a re-render.
class App extends Component {
constructor(){
super();
this.state = {
user: Meteor.user()
}
}
render () {
const { user } = this.state;
if (user && user.check) {
return (<AnotherComponent />)
}
return (<MainComponent />)
}
}
本文标签: javascriptReactJS Reload component if variable changes valueStack Overflow
版权声明:本文标题:javascript - ReactJS: Reload component if variable changes value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741606528a2388009.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论