Answer the question
In order to leave comments, you need to log in
How to pass data to a component from a container in a router?
Perhaps I didn’t formulate the question correctly, now I’ll explain on my fingers what I mean:
Here is my router:
import React from 'react'
import { Route, IndexRedirect } from 'react-router'
import App from './containers/App'
import Publication from './containers/Publication';
import NotFound from './components/NotFound'
import PublicationList from './components/PublicationList';
import PublicationForm from './components/PublicationForm';
import PublicationSetting from './components/PublicationSetting';
export const routes = (
<div>
<Route path='/' component={App}>
<Route path='publications' component={Publication}>
<IndexRedirect to='list' />
<Route path='list' component={PublicationList} />
<Route path='form' component={PublicationForm} />
<Route path='setting' component={PublicationSetting} />
</Route>
<Route path='*' component={NotFound} />
</Route>
</div>
)
this.props.children
the "PublicationList" child component is passed to him. {this.props.children}
{this.props.children}
(PublicationList)? Answer the question
In order to leave comments, you need to log in
https://facebook.github.io/react/docs/context can be used.
class Publication extends React.Component {
state = {
list: []
};
componentDidMount() {
this.timer = setInterval(() => {
this.setState({list: [...this.state.list, new Date().toString()]});
}, 10000);
}
componentWillUnmount() {
clearInterval(this.timer);
}
static childContextTypes = {
list: React.PropTypes.array
};
getChildContext() {
return {
list: this.state.list
};
}
render() {
return (
<div>
<h4>Publication</h4>
{this.props.children}
</div>
);
}
}
const PublicationList = (props, context) => (
<ul>
{context.list.map(e => <li key={e}>{e}</li>)}
</ul>
);
PublicationList.contextTypes = {list: React.PropTypes.array};
for each of them create your own reducer, action, constantsSimple enough
export connect(state => ({list: state.publication.list}))(PublicationList)
method #1
the parent
component
explicitly knows what the child component needs and then the parent component explicitly passes the necessary values through props
One of the main disadvantages: in each parent component, you need to take into account the requirements of all child components at all lower levels use something from its props and state (i.e. from the props and state of the parent component). Therefore, the parent component explicitly passes its state and props to the child element using props
Minus #1 - see method #1
Minus #2 - the state of this parent component becomes public. What is not always good
way #3
the data that is needed for different components is taken out to the global scope (redux, flux, mobx, etc.).
In this case, the parent component updates some part of the public data.
Other components that are interested in this data subscribe to notifications of changes to this data
Minus - an additional level of abstraction
But solves the shortcomings of the previous options
Which is better... Depends on the situation
Well, Redux is just for that?
upd. v.2
componentDidMount() {
this.setState({
// route components are rendered with useful information, like URL params
user: findUserById(this.props.params.userId)
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question