I have one question about the react-way.
I have two components. For example: App & Browser.
- App is a general component. It loads inner modules and renders application tree-structure
- Browser is an inner-component of App. It shows some fetched data by its ID.
App-component doesn't matter about ID and data what currently rendered in Browser-component. App doesn't want to control browser-component's navigation. Browser is mostly independent.
But sometimes App wanna do one of it:
- Ask Browser-component for refresh (fetch data again + render it) active page
- Ask Browser-component to load particular ID
I don't understand how I can do it using react-way with props.
Some code for example:
class App extends Component {
render(){
return ... <Browser server={this.server}/> ...;
}
}
class Browser extends Component {
constructor(props){
super(props);
this.state = { id: 'default' };
}
componentDidMound() { this.checkData(); }
componentWillUpdate() { this.checkData(); }
async checkData(){
if(!this.state.data}{
const data = await this.props.server.fetch(this.state.id);
this.setState({ data });
}
}
onChange(newId){
this.setState({ data: null, id: newId });
}
render(){
return <div>
<Page data={this.state.data}/>
<Navigation activeId={this.state.id} onChange={::this.onChange}/>
</div>;
}
}
I have some bad idea's. Example:
- I can set Browser to App by ref-attribute. And directly run needed methods
- I can use global variables
- I can provide empty {}-object into Browser-component. In initialization in browser-component set all needed methods into this object. Finally run it from App
I think all this variants isn't react-way. How I can do it right? I don't use redux in this project. Just react.