【问题标题】:FlowType React ContextFlowType 反应上下文
【发布时间】:2017-04-12 17:49:24
【问题描述】:

有没有办法让 React Context 对流类型安全?

例如:

Button.contextTypes = {
  color: React.PropTypes.string
};

【问题讨论】:

    标签: flowtype


    【解决方案1】:

    我使用的一种解决方法是在父级从消费者那里提取上下文,然后在子级调用 proptypes...

    父母

    //parent
    class Parent extends component {
    render(){
    return (
        <Consumer>{(context)=>{
           const { color } = context
           return(
              <div>
                 <Button color={color} />
              </div>
            )}}</Consumer>
    }
    

    孩子

    //Button 
    ... 
    Button.contextTypes = {
      color: React.PropTypes.string
    };
    ...
    

    【讨论】:

      【解决方案2】:

      不幸的是,这本质上是不可能的,因为Context 在编译时是未知的 (so I was told)。

      【讨论】:

      猜你喜欢
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 2019-04-20
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      相关资源
      最近更新 更多