javascript - How to use getChildContext with functional components - Stack Overflow

I want to pass some props down the tree via context from a functional ponentthat is almost on the top

I want to pass some props down the tree via context from a functional ponent that is almost on the top of the app. Is there a way to achieve this, or should we declare instead a ES6 / createClass react ponent?

Thanks

I want to pass some props down the tree via context from a functional ponent that is almost on the top of the app. Is there a way to achieve this, or should we declare instead a ES6 / createClass react ponent?

Thanks

Share Improve this question edited Sep 8, 2016 at 15:35 jsdario asked Sep 8, 2016 at 15:24 jsdariojsdario 6,87311 gold badges44 silver badges78 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You can't pass context to children using functional ponents

Define the contextTypes as you would normally do:

const Hello = (props, context) => <div>{context.name}</div>;
Hello.contextTypes = {
  name: React.PropTypes.string
};

A fiddle is available: https://jsfiddle/69z2wepo/55601/.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745175157a4615152.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信