在视图中呈现多个按钮以编程方式进行本机反应 [英] Rendering Multiple Buttons in a View react native programmatically

查看:63
本文介绍了在视图中呈现多个按钮以编程方式进行本机反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要渲染按钮基于json
{
ActionType:[{
Field:Button,
FieldText:No,
FieldAction:this.getCellNumber('no'),
style:
},{
Field:Button,
FieldText:No,
FieldAction:this.getCellNumber('yes'),
style:
}]
}

Want to render buttons Based upon the json { "ActionType" : [{ "Field" : "Button", "FieldText" : "No", "FieldAction" : "this.getCellNumber('no')", "style":"" },{ "Field" : "Button", "FieldText" : "No", "FieldAction" : "this.getCellNumber('yes')", "style":"" }] }

如何在渲染函数中循环它以获得视图中的按钮数量

How can I loop it in a render function to get number of buttons in a view

render(){
  return(

        <View style={this.styles.ButtonValContainer}>

        </View>
);

}


推荐答案

in react你可以轻松地收集一系列元素进行渲染。

in react you can easily collect an array of elements for rendering.

const buttons = [
    {
      text: 'button one',
      action: () => console.log('pressed button one'),
    }, 
    {
      text: 'button two',
      action: () => console.log('pressed button two')            
    }
];

....

// react class definition

// ...
// assuming `Button` is a defined class in scope.

render() {
  const renderedButtons =  buttons.map(b => {
    return <Button key={b.text} text={b.text} onPress={b.action} />;
  });

  return (
    <View>
     {renderedButtons}
    </View>
  )
}

希望有所帮助。

这篇关于在视图中呈现多个按钮以编程方式进行本机反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆