我在 JSX 文件中使用一个组件,并将属性值传递给该组件,如下所示:
anything.jsx
import Example from '/containers/example.js'
class Anything extends Component {
render() {
return (
<Example type='article' />
)
}
}
在下面您可以看到,该组件通过容器填充了数据。现在我需要获取该容器中的属性值,以将其作为订阅值传递。
该组件具有type
值article
。最后,我需要在订阅中获取此值,例如 Meteor.subscription('exampleDetail', mainID, 'article')
。我如何传递该值?
示例组件 (components/example.jsx)
export default class Example extends Component {
constructor(props) {
super(props)
}
render() {
const { type } = this.props
console.log(type);
}
}
示例容器 (containers/example.js)
import Example from '../components/example.jsx'
export default createContainer(() => {
const mainID = Session.get('mainID'),
// ??? How to get type value?
subscription = Meteor.subscribe('exampleDetail', mainID, type)
loading = !subscription.ready()
results = Collection.find().fetch()
return { results, loading }
}, Example)
最佳答案
尝试更改为 createContainer((props) => { console.log(props) ...}
关于javascript - ReactJS:如何获取组件容器中的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44430602/