CodeG
CodeG

Reputation: 467

eslint: Unexpected block statement surrounding arrow body

I have a component in react and I am getting an error lint:

Unexpected block statement surrounding arrow body; move the returned value immediately after the => arrow-body-style

export function dashConfig(config) {
  return (Component) => {
    return (props) => {
      const { data, isLoaded, error } = useDashConfig({ ...config });

      return <Component {...props} remoteConfig={{ data, isLoaded, error }} />;
    };
  };
}

Which return should I remove? I didn't quite understand the explanation. Thanks!

Upvotes: 0

Views: 439

Answers (3)

W Kenny
W Kenny

Reputation: 2089

if there is only one return in the statement, we should remove the return like the following example.

res => {
    return new Promise().resolve(null);
}

Should be in this way

res => (new Promise().resolve(null)); 

To your case, it should be like the following

export function dashConfig(config) =>
  Component =>
    props => {
      const { data, isLoaded, error } = useDashConfig({ ...config });
      return <Component {...props} remoteConfig={{ data, isLoaded, error }} />;
    };
  };
}

Upvotes: 0

zero298
zero298

Reputation: 26930

You are writing an arrow function body that just immediately returns:

function foo() {
  console.log("hello");
  return () => {
    return () => {
      console.log("world");
    }
  }
}

// Is the same as below

function bar() {
  console.log("hello");
  return () => () => {
    console.log("world");
  }
};

foo()()();
bar()()();

This applies to your own code in the following:

export function dashConfig(config) {
  return (Component) => {
    return (props) => {
      const { data, isLoaded, error } = useDashConfig({ ...config });

      return <Component {...props} remoteConfig={{ data, isLoaded, error }} />;
    };
  };
}

// Is the same as this

export function dashConfig(config) {
  return (Component) => (props) => {
    const { data, isLoaded, error } = useDashConfig({ ...config });

    return <Component {...props} remoteConfig={{ data, isLoaded, error }} />;
  }
}

Upvotes: 0

Pablo Darde
Pablo Darde

Reputation: 6432

export function dashConfig(config) =>
  Component =>
    props => {
      const { data, isLoaded, error } = useDashConfig({ ...config });

      return <Component {...props} remoteConfig={{ data, isLoaded, error }} />;
    };
  };
}

Upvotes: 2

Related Questions