JavaScript Return JSX from function return from function python,return from function bash,return from

I have a a component that needs to check for multiple options and return based on those options. Therefore I have created an outside function that is called in my component return statement to determine the format that will be returned:

render() {
    const { policy } = this.props;
    let deployment = policy.Deployment;
    let value = policy.value;
    let policyLegend = deployment.policyLegend;
    let policyObj = this.valueToPolicy(policyLegend, value);
    console.log(policyObj);
    if(policy.name == "SP4") {
      policyLegend[1].values = this.formatSp4Firmware(policyLegend[1]);
      return (
        <div>
          <Form onSubmit={ (event) => this.handleSubmit(event, this.props) }>
            {
              policyLegend.map((policy) => {
                return (
                  <div key={ policy.id }>
                    <h3>{ policy.displayName }</h3>
                    { this.renderSp4Policies(policy) }
                  </div>
                );
              })
            }
            <Button name={ 'Submit' } type='submit'>Submit</Button>
            <Button onClick={ this.props.onCancel }>Cancel</Button>
          </Form>
        </div>
      )
    } 
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
Answer:1

Your map function doesn't return anything:

policy.values.map(value => {
            <Form.Field key={ value.name }>
              <label>{ value.displayName || value.name }</label>
              <Checkbox toggle />
            </Form.Field>
          } 

When using an arrow function block {} you should add an explicit return statement:

policy.values.map(value => {
            return(<Form.Field key={ value.name }>
              <label>{ value.displayName || value.name }</label>
              <Checkbox toggle />
            </Form.Field>)
          }

Edit
As a followup to your comment.

As for the first if statement, don't use a forEach, use a map. same as you did in the second if statement (don't forget the return!)

renderSp4Policies(policy) {
  if (policy.displayName == "Firmware Options") {
    let toggles = policy.values[0][Object.keys(policy.values[0])];
    Object.keys(toggles).forEach(toggle => {
      if (typeof (toggles[toggle]) !== "boolean") {
        delete toggles[toggle];
      }
    })
    return (
      Object.keys(toggles).map(toggle => {
        return (
          <Form.Field key={toggle}>
            <label> {toggle} </label>
            <Checkbox toggle />
          </Form.Field>
        );
      })
    )
  } else {
    return (
      policy.values.map(value => {
        <Form.Field key={value.name}>
          <label>{value.displayName || value.name}</label>
          <Checkbox toggle />
        </Form.Field>
      })
    );
  }
}
Answer:2

I run into this quite a bit and didn't just realize why this works until now. In your renderSp4Policies function, you're mapping through keys on the second line that has Object.keys(toggles).forEach(toggle => {. Inside that block you're returning elements and that instruction you're running is keeping track of that and generating an array. Once it's done, you're not doing anything with the array, so you have to add a return statement so the function returns the generated array of JSX elements:

return Object.keys(toggles).map(toggle => {
    return(
      <Form.Field key={ toggle }>
        <label> { toggle } </label>
        <Checkbox toggle />
      </Form.Field>
    );
  });

Also as Sag1v says, the line that has policy.values.map, you're not returning inside that loop. So you have to change:

policy.values.map(value => (
  return (
    <Form.Field key={ value.name }>
      <label>{ value.displayName || value.name }</label>
      <Checkbox toggle />
    </Form.Field>
  );
));

Edit

After our discussion in chat, I wanted to update my answer to reflect the proper changes.

The first Object.keys call that determines which keys should be saved and which shouldn't wasn't returning new data after every iteration so it was just making an empty array. forEach was also being used but that doesn't return a new array. After changing forEach to map and adding some return statements, it would create a new array so the second loop that follows would be able to run:

var toggleOptions = Object.keys(toggles).map(toggle => {
  if (typeof (toggles[toggle]) !== "boolean") {
    return delete toggles[toggle];
  }
  return toggles[toggle];
})
Answer:3

Just change this function with map arrow function return.

renderSp4Policies(policy) {
    if(policy.displayName == "Firmware Options") {
      let toggles = policy.values[0][Object.keys(policy.values[0])];
      Object.keys(toggles).forEach(toggle => {
        if(typeof(toggles[toggle]) !== "boolean"){
            delete toggles[toggle];
        }
      })
      Object.keys(toggles).forEach(toggle => {
        return(
          <Form.Field key={ toggle }>
            <label> { toggle } </label>
            <Checkbox toggle />
          </Form.Field>
        );
      })
    } else {
        return (
          policy.values.map(value => (
            <Form.Field key={ value.name }>
              <label>{ value.displayName || value.name }</label>
              <Checkbox toggle />
            </Form.Field>
          ))
        );
      }
  }
Answer:4

I have a Web API and I'm trying to get JSON Data from it by using Vue, but I get neither data or errors, so I don't what is wrong. I want to load the data when the page is loaded. Here is my code: ...

I have a Web API and I'm trying to get JSON Data from it by using Vue, but I get neither data or errors, so I don't what is wrong. I want to load the data when the page is loaded. Here is my code: ...

  1. fetch data with react hooks
  2. fetch data with react
  3. fetch data with axios
  4. fetch data with javascript
  5. fetch data with redux
  6. fetch data with ajax
  7. fetch data with redux thunk
  8. fetch data with mysqli php
  9. fetch data with ajax in php
  10. fetch data with usereducer
  11. fetch data with pdo
  12. fetch data with jquery
  13. fetch data with redux saga
  14. fetch data with react redux
  15. fetch data with php
  16. fetch data with json
  17. fetch data with angular
  18. fetch data with post
  19. fetch data with jquery ajax
  20. fetch data with sql

I am facing a little problems with using Custom Palettes with Angular Material Framework. I still not understand myself to use custom theme. In angular config file. $mdThemingProvider.definePalette('...

I am facing a little problems with using Custom Palettes with Angular Material Framework. I still not understand myself to use custom theme. In angular config file. $mdThemingProvider.definePalette('...

How can I detect width and height of the camera to use it on canvas and maintain the proportions?? I need to detect width and height of the camera and then use it on a canvas. In the new canvas I ...

How can I detect width and height of the camera to use it on canvas and maintain the proportions?? I need to detect width and height of the camera and then use it on a canvas. In the new canvas I ...

  1. detect height width

I have a requirement where all the checked values in checkboxes should be displayed in a textarea. This part works perfectly fine, but the problem occurs when I uncheck the checkbox unchecked element ...

I have a requirement where all the checked values in checkboxes should be displayed in a textarea. This part works perfectly fine, but the problem occurs when I uncheck the checkbox unchecked element ...

  1. pivot table remove values heading
  2. pivot table remove values less than
  3. excel pivot table remove values
  4. pivot table remove values
  5. excel table remove values
  6. remove values from table sql
  7. remove values from table
  8. remove values header pivot table
  9. remove values from table mysql
  10. remove values from table r
  11. remove values from pivot table vba