我的所有组件都包含在react redux中,我试图通过点击某个按钮从另一个组件渲染一些组件,但我无法获得传递的道具
这是我尝试使用一些道具渲染子组件的代码:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import ChildComponent from './child_component';
class Home extends Component {
constructor(props) {
super(props);
}
go_to_child_component(){
this.setState({
child_component_triggered: true
})
}
render() {
return (
<div>
{this.state.child_component_triggered ? <ChildComponent filter="some_value" /> : null}
<button onClick={this.go_to_child_component.bind(this)}>render ChildComponent</button>
</div>
)
}
const mapStateToProps= (state) => {...//normal codes}
const mapDispatchToProps = (dispatch) => {...//normal codes}
export default connect(mapStateToProps, mapDispatchToProps)(Home);
这是我试图通过ownProps获取子组件中传递的道具的地方:
import React, { Component } from 'react';
import { connect } from 'react-redux';
class ChildComponent extends Component {
constructor(props) {
super(props);
}
render() {
return (
<div> i am child component </div>
)
}
}
const mapStateToProps = (state, ownProps) => {
console.log(ownProps) // always {}
let obj = {...state};
obj.ownProps = {...ownProps};
return {...obj};
};
const mapDispatchToProps = (dispatch) => {...//normal codes}
export default connect(mapStateToProps, mapDispatchToProps)(ChildComponent);
ownProps总是空对象,请帮忙
答案 0 :(得分:1)
您可以在子组件中使用this.props.filter而无需maping