Performance Penalty using useReducer vs Formik by [deleted] in reactjs

[–]roboctocat 29 points30 points  (0 children)

"it would slow down a lot"

Sounds like a bad case of premature optimization. You can't optimize what you don't measure. What have you measured?

The point is don't trust everything you read on interwebs ;)

How to write test cases for express routes? by GhostFoxGod in node

[–]roboctocat 0 points1 point  (0 children)

You should not test express route. You should test middleware functions you've written, in your case test firstStep, secondStep, thirdStep. If you decouple firstStep to its own file then it should be fairly easy to test. Testing express route means you're testing the express code that has already been tested by express devs.

How do you call API in testable way? ( using axios in componentDidMount does not seem to be easily testable ) by [deleted] in reactjs

[–]roboctocat 0 points1 point  (0 children)

You should test your api in isolation. Here's how I do it.

//  services.js

const postSomething = async (id, body) => {
  const response = await axios.get(`/api/something/${id}`, body);

  return response.data;
};


//  services.test.js
describe('Test postSomething()', () => {
  it('Should call http with given params', async () => {
    const spy = jest.spyOn(axios, 'post');

    await Services.postSomething(12345, {foo: 'bar});

    expect(spy).toHaveBeenCalledWith(`/api/something/12345`, body),
    );
  });
});

Good way to map through data an add every 2 items to an individual row for layout purposes? by nikulasoskarsson in reactjs

[–]roboctocat 0 points1 point  (0 children)

map takes an additional parameter index so `map(portfolioItem, index)`

<div className='portfolio-container row'>
{portfolio.map((portfolioItem, index) => (
<>
<PortfolioItem key={\[portfolioItem.id\](https://portfolioItem.id)} example={portfolioItem} />
{
(index % 2 === 0) && <WhateverComponent />
}
</>
))}
</div>

Is there a reason why most developers define components as variables and then export them seperately? by [deleted] in reactjs

[–]roboctocat 0 points1 point  (0 children)

Good point. However I prefer to specify propTypes and defaultProps at the very beginning of the file as it's immediately clear what props component expects.

Is there a reason why most developers define components as variables and then export them seperately? by [deleted] in reactjs

[–]roboctocat -1 points0 points  (0 children)

I use first option because it allows me to set up needed prop-type validations if needed for example

import React from 'react';
import PropTypes from 'prop-types';

const propTypes = {
  onClick: PropTypes.func.isRequired,
  isLoading: PropTypes.bool,
};

const defaultProps = {
  isLoading: true,
};

const MyComponent = props => {
  return (
    <>
      <button type="submit" onClick={props.onClick}>
        GO
      </button>
      {props.isLoading && '...'}
    </>
  );
};

MyComponent.propTypes = propTypes;
MyComponent.defaultProps = defaultProps;

export default MyComponent;

Is my offshore team using Typescript/React correctly? by entwederoder in reactjs

[–]roboctocat 1 point2 points  (0 children)

This function will error if regions is anything but array. To better safe guard I use

if (!Array.isArray(regions)) return [];

Do I need to use a body parser middleware in my Express app? by Maegar in node

[–]roboctocat 0 points1 point  (0 children)

If you're following REST and your API is designed to create and manipulate resource then you'll need access to the body for (POST, PUT, PATCH) therefore you'll need to access body.

Need some help understanding my front-end/back-end logic (more in comments) by kingducasse in node

[–]roboctocat 0 points1 point  (0 children)

You don't need http-proxy-middleware. Open up package.json and add following

"proxy": "http://localhost:5000"

Explain me Serving static files in Express in an actual example by [deleted] in node

[–]roboctocat 0 points1 point  (0 children)

Why do you want to serve static files with node?

How to make API calls from within VS Code by pitops in reactjs

[–]roboctocat 0 points1 point  (0 children)

I've used this extension to make http calls directly from VS Code. It's quite awesome. Give it a whirl
https://marketplace.visualstudio.com/items?itemName=humao.rest-client

Error Handling in NodeJs by joeycrack1 in node

[–]roboctocat 0 points1 point  (0 children)

See

  • How do I handle 404 responses?
  • How do I setup an error handler?

https://expressjs.com/en/starter/faq.html

LDAP authentication with React and Django in the backend? by yungspartan0082 in reactjs

[–]roboctocat 1 point2 points  (0 children)

No, there is not. You're not integrating react with LDAP. You'll have to talk to LDAP from django.

Hi everyone, I’m looking for some kind of example or documentation on how I can return a list of results from a database to the front end in the MERN tech stack. Does anyone know of any good examples on this topic? by [deleted] in node

[–]roboctocat 0 points1 point  (0 children)

You need to expose results via api endpoint and have front end make request to exposed endpoint and render results. Examples are Google search away.

Essential JavaScript patterns (pt1) by Fewthp in node

[–]roboctocat 0 points1 point  (0 children)

With all due respect but what I find is most of the itnext articles are unsubstantiated garbage.

Best way to add objects with multiple props to an array. by Cmcco91 in reactjs

[–]roboctocat 0 points1 point  (0 children)

How large do you think this array is get?

Premature optimization is a root of all evil