是的,這是允許的情況 - 但是,我們也許能夠實現這個功能有點清潔。例如,在mapStateToProps
接受react-redux
第二個參數,題爲ownProps
,其是傳遞到容器組件所討論的道具:
通過https://github.com/reactjs/react-redux/blob/master/docs/api.md
[mapStateToProps(state, [ownProps]): stateProps]
(功能): ...如果ownProps被指定爲第二個參數,它的值將是傳遞給你的組件的 道具,並且只要組件接收到新的道具,就會重新調用mapStateToProps 。
因此,也許不是實例fooBar
您的容器組件內,爲什麼不把它作爲一個道具組件本身,然後通過ownProps
訪問它?
一些其他部件
<MyContainer fooBar={() => alert('foobar!')}
myContainer中
const mapStateToProps = (state, ownProps) => {
return {
todos: getVisibleTodos(state.todos, state.visibilityFilter),
fooBar: ownProps.fooBar
}
}
來源
2016-03-08 20:12:28
lux
你可以做到這一點。您希望收到關於「fooBar」更改時間的通知,因爲它不會通過Redux進行更改 – Andreyco