class MyD3Component extends Component {
componenDidMount() {
ReactDOM.findDOMNode(this.refs.container) // для d3
}
render() {
return <div ref="container" />
}
}
class Container extends React.Component {
constructor(props){
super(props);
}
componentDidMount(){
console.log('componentDidMount', Object.keys(this.refs));
// outputs: [ 'ch0', 'ch1' ]
}
render(){
return (
<div>{React.Children.map(this.props.children, (ch, i) => React.cloneElement(ch, { ref: 'ch' + i }))}</div>
);
}
}
ReactDOM.render(
<Container>
<div>hello</div>
<div>world</div>
</Container>,
document.querySelector('.main-container')
);
function getChildren(children) {
let elements = []
if (!children) {
return elements
}
React.Children.forEach(children, child => {
elements = [ ...elements, child, ...getChildren(child.props.children) ]
})
return elements
}
componentWillReceiveProps(nextProps) {
this.setState({ id: nextProps.id })
}
const myReducer = combineReducers({ connectedReducer1, connectedReducer2 })
function combinedReducer(state = initialState, action = {}) {
switch(action.type) {
case SOME_COMPLEX_ACTION:
return {
...state,
someProp: someOtherReducer(state.someProp, { type: OTHER_ACTION, payload: action.payload.something })
}
default:
return {
...state,
someProp: someOtherReducer(state.someProp)
}
}
}
function mapStateToProps(state) {
const filter = state.something.filter
const items = state.other.items
return {
items: filterItems(items, filter)
}
}
connect(mapStateToProps)(...)
<Router history={hashHistory}>
<Route path="/" component={UserHome}>
<Route path="NewPatient" component={NewPatient} />
</Route>
</Router>
return (
<div className="UserHome ">
{this.props.children && React.cloneElement(this.props.children, {
patientUr: this.state.patient_ur
})}
</div>
)
class MyContainer extends Component {
constructor() {
this.state = { nums: [] }
}
componentDidMount() {
this.intervalId = setInterval(() => {
this.setState({
nums: [
...this.state.nums,
(this.state.nums[this.state.nums.length-1] || 0) + 1
],
})
}, 1000)
}
componentWillUnmount() {
clearInterval(this.intervalId)
}
render() {
retrurn (
<div>
{this.state.nums.map((n, i) => (<b key={i}>Number is: {n}</b>)}
</div>
)
}
}
Каждый раз, когда мне нужно вызвать событие с уведомлением (будь то новое уведомление или удаление старого), мне нужно доставать имеющийся массив this.props.layout.notification, модифицировать его и отправлять обратно для перезаписи
dispatch({
type: 'ADD_NOTIFICATION',
payload: { notification: { ... } },
})
// rootEntity reducer
const initialState = {
entities: {}
}
function rootEntityReducer(state = initialState, action = {}) {
switch(action.type) {
case ADD_ELEMENT:
const { targetEntityId, element } = action.payload
const targetEntity = state.entities[targetEntityId]
return {
...state,
entites: {
...state.entities,
[targetEntityId]: { ...targetEntity, elements: [ ...targetEntity.elements, element ] }
}
}
default:
return state
}
}
// rootEntity reducer
const initialState = {
entities: []
}
function rootEntityReducer(state = initialState, action = {}) {
switch(action.type) {
case ADD_ELEMENT:
const { targetEntityId, element } = action.payload
return {
...state,
entites: state.entities.map(
entity => entity.id !== targetEntityId
? entity
: { ...entity , elements: [ ...entity.elements, element ] }
)
}
default:
return state
}
}