例如:
在Link.js中
class Link extends React.Component {
...
}
export default Link
Run Code Online (Sandbox Code Playgroud)
在LinkContainer.js中
import { connect } from 'react-redux'
import Link from './Link'
const mapStateToProps = (state, ownProps) => {
return {
data: state.data
}
}
const LinkContainer = connect(
mapStateToProps
)(Link)
export default LinkContainer
Run Code Online (Sandbox Code Playgroud)
在App.js. 我有一个onClick方法来动态呈现链接.
class App extends React.Component {
...
onClick() {
ReactDOM.render(<LinkContainer />,document.getElementById('link1')) //It doesn't work
// ReactDOM.render(<Link />,document.getElementById('link1')) //It works
}
}
Run Code Online (Sandbox Code Playgroud)
当我渲染容器时,错误发生了
未捕获的不变违规:无法在"连接(链接)"的上下文或道具中找到"存储".将根组件包装在一个,或者显式地将"store"作为prop传递给"Connect(Link)"