最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Rendering Multiple Buttons in a View react native programmatically - Stack Overflow

programmeradmin4浏览0评论

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>
);

}

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>
);

}
Share Improve this question edited May 7, 2016 at 15:36 Nilesh asked May 7, 2016 at 15:28 NileshNilesh 1671 gold badge4 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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>
  )
}

hope that helps a bit.

You can iterate through a list and return a ponent in the callback. The only requirement is that the returned ponent has an key attribute, which is used by React internally when rerendering (so that it knows what to remove/shift/update etc)

render() {
  return(
    <Wrapper>
      {items.forEach(function(item) {
        return (
          <View style={this.styles.ButtonValContainer} key={'UNIQUE_KEY_HERE'}>
            ...
          </View>
        );
      })}
    </Wrapper>
  );
}

Here's some more info: https://facebook.github.io/react/docs/multiple-ponents.html

发布评论

评论列表(0)

  1. 暂无评论