简体   繁体   English

setState()如何取决于调用哪个组件函数?

[英]How to setState() depend on which component func is called?

I want to set dynamic state depending on which function is called, I know if we can't setstate in render, but still i need to do that to set dynamic state, 我想设置取决于哪个函数被调用动态状态,我知道,如果我们不能setstate在渲染,但我仍然需要做的是设置动态状态,

there's some way to make it possible? 有办法使之成为可能吗?

export default class Foo extends Component {
  constructor(props) {
    super(props);
    this.state={
      dynamicView:false
    }
  }
  renderText(key, value){
    this.setState({[key]:value})
    <Text>Simple render</Text>
  }
  renderButton(key, value){
    this.setState({[key]:value})
    <Text>Simple render</Text>
  }
  render(){
    return(
      <View>
        {this.state.dynamicView ? this.renderButton("button","ValueButton") : this.renderText("text", "valueText")}
        <Button
          title="change Component"
          onPress={()=>this.setState({dynamicView:!this.state.dynamicView})}
        />
        <Button
          title="Isi State"
          onPress={()=>alert(JSON.stringify(this.state,null,4))}
        />
      </View>
    )
  }
}

with those code I can set dynamic state, but the problem is while both of component function is called, i have two state (button and text) , i want to avoid that, so i just have 1 state (button / text) depending on which component is display, 使用这些代码,我可以设置动态状态,但是问题是同时调用了两个component function ,我有两个状态(button and text) ,我想避免这种情况,所以我只有1个状态(button / text)具体取决于显示哪个组件,

how can i do that? 我怎样才能做到这一点?

Note: this is just a simple use-case, all i need to know is to set state depend on which function is called 注意:这只是一个简单的用例,我所需要知道的是根据调用哪个函数来设置状态

If you want to keep just either button or text state then you should consider changing the other state. 如果只想保留按钮状态或文本状态,则应考虑更改其他状态。

    renderText(key, value){
    this.setState({[key]:value})
    this.setState({button:false})
    <Text>Simple render</Text>
  }

I think you need to have another variable in state, and update that based on the function fired up based on the condition of dynamicView. 我认为您需要状态中的另一个变量,并根据基于dynamicView的条件触发的功能对其进行更新。

export default class Foo extends Component {
  constructor(props) {
  super(props);
   this.state={
     dynamicView:false,
     viewType:''
   }
 }
  renderText(viewType){
    this.setState(viewType)
    <Text>Simple render</Text>
  }

 renderButton(viewType){
   this.setState({viewType})
   <Text>Simple render</Text>
  }

 render(){
   return(
    <View>
     {this.state.dynamicView ? this.renderButton("button","ValueButton") : 
      this.renderText("text", "valueText")}
      <Button
       title="change Component"
       onPress={()=>this.setState({dynamicView:!this.state.dynamicView})}
       />
      <Button
       title="Isi State"
       onPress={()=>alert(JSON.stringify(this.state,null,4))}
      />
   </View>
  )
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “在构建过程中调用了 setState() 或 markNeedsBuild()”如何解决这个问题? - "setState() or markNeedsBuild() called during build" how to resolve this? 如何包含依赖于同一个库的库? - How to include libraries which both depend on the same library? 如何调用此组件以及如何实现它 - How this component is called, and how to implement it Dagger 2 Singleton组件取决于Singleton - Dagger 2 Singleton Component Depend On Singleton FabricViewStateManager:在没有 StateWrapper 的情况下调用 setState - FabricViewStateManager: setState called without a StateWrapper 将对象转换为特定类取决于何时调用 - cast an object into specific class depend on when called 如何修改Android程序依赖的第三方包名? - How to modify the third-party package name on which Android programs depend? Gitlab CI :- 如何在不依赖任何系统的 Gitlab 中创建共享运行程序? - Gitlab CI :- How to create the Shared Runner in Gitlab which does not depend on the any system? 单例组件不能依赖作用域组件 - Singleton component cannot depend on scoped components 如何实现调用哪个上下文菜单 - how to realize which context menu is called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM