2

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

}

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @mason505 , this helps.
I believe there is a typo in the button array-button two is named 'test' whereas i think it should be 'text'.
you are correct. I'll will update the answer, thank you
1

You can iterate through a list and return a component in the callback. The only requirement is that the returned component 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-components.html

1 Comment

Getting this error: ExceptionsManager.js:61 Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.