Categories
React Tips

React Tips — styled-components, Batch Updates, Child Validation

React is a popular library for creating web apps and mobile apps.

In this article, we’ll look at some tips for writing better React apps.

React Batch State Update Batching with Hooks

If the state changes and triggered asynchronously, then they won’t be batched.

On the other hand, if they’re triggered directly, then they’ll be batched.

For instance, if we call multiple state change functions within a promise, then they won’t be batched:

import React, { useState } from 'react';
import ReactDOM from 'react-dom';

function Component() {
  const [foo, setFoo] = useState('a');
  const [bar, setBar] = useState('b');

  const handleClickPromise = () => {
    Promise.resolve().then(() => {
      setFoo('aa');
      setBar('bb');
    });
  }

  return (
    <>
      <button onClick={handleClickPromise}>
        click me
      </button>
      <p>{foo} {bar}</p>
   </>
  );
}

If the state change functions are called in a promise as we have in the then callback, then they won’t be batched.

On the other hand, if we have:

import React, { useState } from 'react';
import ReactDOM from 'react-dom';

function Component() {
  const [foo, setFoo] = useState('a');
  const [bar, setBar] = useState('b');

  const handleClick = () => {
    setFoo('aa');
    setBar('bb');
  }

  return (
    <>
      <button onClick={handleClick}>
        click me
      </button>
      <p>{foo} {bar}</p>
   </>
  );
}

Then they’ll be batched.

If they’re batched, then the updates will show up immediately.

Conditional Rendering in styled-components

To render something conditionally with a component created with styled-components, we can pass in a function into the string with the props as the parameter.

For instance, we can write:

const Button = styled.button`
  width: 100%;
  outline: 0;
  border: 0;
  height: 100%;
  justify-content: center;
  align-items: center;
  line-height: 0.2;

  ${({ active }) => active && `
    background: green;
  `}
`;

We have a bunch of static styles.

In the end, we have a function that takes the active prop.

Then we return 'background: green' with active is true .

The styled.button template tag is a function that’ll convert the style string to a component with the styles.

And then can use it by writing:

<Button active={this.state.active}></Button>

We can also add conditional styles to an existing component by writing:

const StyledFoo = styled(Foo)`
  background: ${props => props.active ? 'blue' : 'green'}
`

We use the styled function which takes a component and returns a template tag.

Then we create a string with a function interpolated in it that takes the props and returns 'blue' or 'green' depending on if the active prop is true or not.

Only Allow Children of a Specific Type in a React Component

To allow children of a specific type in a React component, we can loop through each entry of children and then throw an error if we see something we don’t want.

To do that, we can create a custom validation function in the object that we set as the value of the propTypes property.

For instance, we can write:

class ItemGroup extends Component {
  render() {
    return (
      <div>{this.props.children}</div>
    )
  }
}

ItemGroup.propTypes = {
  children(props, propName, componentName) {
    const prop = props[propName]
    let error = null
    React.Children.forEach(prop, (child) => {
      if (child.type !== Item) {
        error = new Error(`${componentName} children should be Items`);
      }
    })
    return error;
  }
}

We created our own prop validation method to validate the children prop.

We get all the children with React.children and call forEach on it to loop through each item.

In the callback, we get the child element that we’re looping through and we can check the type of it with the type property.

If it doesn’t return something of type Item , we set the error and return it.

Prevent Multiple Button Presses with React

To prevent multiple button presses with React, we can use the disabled prop to disable the button after we clicked the button.

For instance, we can write:

class App extends React.Component {
  this.state = {
    disabled : false
  };

  handleClick = (event) => {
    if (this.state.disabled) {
      return;
    }
    this.setState({ disabled: true });
  }

  render() {
    return (
     <button onClick={this.handleClick} disabled={this.state.disabled}>
      {this.state.disabled ? 'Sending...' : 'Send'}
     <button>
    );
}

We have the handleClick method that checks the disabled state.

If it’s true , we do nothing.

Otherwise, we set thedisabled state to true and the button will be disabled because we passed that value to the disabled prop.

Conclusion

We can disable buttons on click with the disabled prop.

State updates are batched if they’re called synchronously in function components.

Styling can be done conditionally with styleled-components.

Categories
React Tips

React Tips — Query Params, Inputs, Multiple Routes, Hooks vs Lifecycle Methods

React is a popular library for creating web apps and mobile apps.

In this article, we’ll look at some tips for writing better React apps.

Programmatically Update Query Params in React Router

We can update query parameters with React Router by calling history.push .

We can either call it with an object:

history.push({
  pathname: '/cars',
  search: '?color=green'
})

or:

history.push('/car?color=green')

Change Event for contentEditable

Instead of listening to the change event to listen for content changes in a contentEditable element, we listen to the input event.

For instance, we can write:

<div
  contentEditable
  onInput={e => console.log(e.currentTarget.textContent)}
>
  foo bar
</div>

We pass an event handler to the onInput prop.

Then we can get the value of the content with e.currentTarget.textContent .

ReactJS Lifecycle Method Inside a Function Component

We replace the lifecycle methods with hooks in a function component.

useEffect is equivalent to the lifecycle hooks.

And useState is equivalent to setState .

For instance, we can write:

const Grid = (props) => {
  const [data, setData] = useState();

  const getData = () => {
    const data = await fetchData();
    setData(data);
  }

  useEffect(() => {
    getData();
  }, []);

  return(
    <div>
      {data.map(d => <Row data={data} />)}
    </div>
  )
}

We have the useState hook which returns the array with the latest state value and function to set the state.

Then we have the useEffect hook, which lets us commit side effects.

The 2nd argument is the values to watch for.

If it’s empty, then the callback in useEffect only runs when the component is mounted.

We can have multiple useEffect hooks in one component, unlike the lifecycle methods for class components.

Then we render the items in the JSX expression we return.

The equivalent of componentWillUnmount is the function that we return in the useEffect callback.

For instance, we can write:

useEffect(() => {
  window.addEventListener('click', handler);
  return () => {
    window.removeEventListener('click', handler);
  }
}, [])

We call any code that clears up resources in the function we return in the useEffect callback.

The equivalent of componentDidUpdate is passing in values to the array in the 2nd argument of useEffect .

For instance, if we want to watch for the change in values of the foo state and bar prop, we can write:

useEffect(() => {
  //...
}, [foo, props.bar])

We just pass them into the array and then we always get the latest value.

Can’t Type into React Input Text Field

If we want to be able to type into an input text field, then we’ve to make it a controlled input.

This means we set a state’s value with the latest entered value in an event handler.

And we pass that into the onChange prop.

And we set the value prop to the value of that state.

For instance, we write:

<input
  type="text"
  value={this.props.value}
  onChange={this.handleChange}
/>

to do that.

For a full example, we can write:

class Form extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: ''};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({ value: event.target.value });
  }

  handleSubmit(event) {
    alert(this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          <input type="text" value={this.state.value} onChange={this.handleChange} />
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

The handleChange method sets the latest value that’s entered, which is stored in event.target.value as the value of the value state.

Then we can get that in the handleSubmit method when we click on Submit.

Multiple Path Names for the Same Component in React Router

We can assign multiple paths to redirect to the same path by passing in an array with all the path names to the path prop.

For instance, we can write:

<Router>
  <Route path={["/home", "/user", "/profile"]} component={Home} />
</Router>

We just pass them into the Router component’s path prop and they’ll all match.

Each pat in the array is a regex string.

Conclusion

Multiple path names can be assigned to go to the same component.

We can use history.push to update query parameters.

There are equivalents to component lifecycle methods in class components in function components.

To let us type into an input, we’ve to set the state with the latest inputted value and the set that as the value.

Categories
React Tips

React Tips — Props, this Binding, and Side Effects

React is a popular library for creating web apps and mobile apps.

In this article, we’ll look at some tips for writing better React apps.

Conditionally Pass Prop Inline to a Component

We can conditionally pass prop inline to a component by passing in an expression as the prop value.

For instance, we can write:

<Child {...(this.props.canEdit ? { editOptions : this.state.options } : undefined)} >

We check the canEdit prop to see if it’s truthy.

If it is, then we pass in the editOptions prop to the child.

Otherwise, we passed in undefined .

We use the spread operator to spread the returned objects into props.

Use One or Many useEffect Hooks in a React Component

We can use the useEffect hook as many times as we want.

For instance, we can write:

useEffect(() => {
  // run code
  return () => {
    // run clean up code
  }
}, []);

useEffect(() => {
  // run code when props.foo changes
  return () => {
     // run clean up code when props.foo changes
  }
}, [props.foo])

The first useEffect call is used for running code when the component loads.

The function we return is run when the component unmounts.

In the 2nd useEffect call, we run code when props.foo changes.

And the function that’s return runs clean up code when props.foo changes.

It’s useful for triggering any side effects like an API call or anything like that.

Pass Properties Object to Child Component

We can pass an object’s properties to a child component by using the object spread operator.

For instance, we can write:

return <Child {...props} />;

The all the properties in the props object will be passed into Child as props.

Avoid Binding ‘this’ to Every Method

We can avoid binding all our methods to this by using class fields.

This isn’t part of the JavaScript syntax, but it can be used with Babel or TypeScript.

For instance, we can wire:

class Foo extends React.Component {

  onClick = () => {
    console.log('clicked');
  };

  onMouseOver = () => {
    console.log('mouse over');
  };

  render() {
    return (
      <div
        onClick={this.onClick}
        onMouseOver={this.onMouseOver}
      />
    );
  }

}

It’s still in Stage 3, so it’s not final.

Push Method in React Hooks

We can’t use the push method to append an item to an array since it returns the item that’s appended and changes the array in place.

Instead, we’ve to return a new array with the item that we wan tot append.

For instance, we can write:

const [array, setArray] = useState(initialArray);

Then we write:

setArray(oldArray => [...oldArray, newItem]);

We get the old array and return a new array with the items appended.

The existing items are spread into the new array with the spread operator.

PropTypes Check Object with Dynamic Keys

We can use a function to check an object with dynamic keys when we validate props.

For instance, we can write:

someProp: (props, propName, componentName) => {
  if (!/match/.test(props[propName])) {
    return new Error('match prop doesn't exist');
  }
}

We check if the value of someProp if the value of someProp matches the regex pattern we specified.

If it doesn’t then we throw an error.

props has the props as an object.

propName has the name of the prop.

componentName has the component name.

React propTypes: objectOf vs shape

The objectOf method methods lets us check if a prop value is of a certain type.

For instance, we can write:

number: PropTypes.objectOf(PropTypes.number)

to check if the value of the number prop is an object.

On the other hand, shape lets us check for the structure of an object.

For instance, we can write:

styleObj: PropTypes.shape({
  color: PropTypes.string,
  fontSize: PropTypes.number
}),

Then the styleObj prop has to have the color property, which is a string.

And it should have a fontSize property which is a number.

Conclusion

There are various ways to check the shape of an object we pass in as props.

We can pass properties into an object optionally by specifying a ternary expression.

Also, we can use useEffect multiple times in our component.

Dynamic keys can also be checked with custom functions with the prop-types package.

Categories
React Tips

React Tips — Sharing Data

React is a popular library for creating web apps and mobile apps.

In this article, we’ll look at some tips for writing better React apps.

Update React Context from Inside a Child Component

We can update the React context by first creating a context, and wrapping the child component in the context consumer.

Then we can use the function returned by the useContext hook in the child component to do the update.

For instance,e we can write:

const NameContext = React.createContext({
  name: "",
  setName: () => {}
});

const NameSwitcher = () => {
  const { name, setName } = useContext(NameContext);
  return (
    <button onClick={() => setName("mary")}>
      change name
    </button>
  );
};

const App = () => {
  const [name, setName] = useState("");

  return (
    <NameContext.Provider value={{ name, setName }}>
      <h2>Name: {name}</h2>
      <div>
        <NameSwitcher />
      </div>
    </NameContext.Provider>
  );
};

First, we create the NameContext with the React.createContext method.

Then, we have the App component, which has the name state and setName to update the name state.

We then have the NameContext.Provider wrapped around all the component that we want to have access to NameContext .

The nesting can be at any level.

Everything in the value prop will be returned by useContext when we use it.

Therefore, in NameSwitcher , we can use the setName function to set the name state, which is also returned by useContext .

In class components, we can do the same thing.

For instance, we can write:

const NameContext = React.createContext({
  name: "",
  setName: () => {}
});

class NameSwitcher extends Component {
  render() {
    return (
      <NameContext.Consumer>
        {({ name, setName }) => (
          <button onClick={() => setName("mary")}>
            change name
          </button>
        )}
      </NameContext.Consumer>
    );
  }
}

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      language: "en",
      setLanguage: this.setLanguage
    };
    this.setName = this.setName.bind(this);
  }

  setName(name) {
    this.setState({ name });
  };

  render() {
    return (
      <NameContext.Provider value={{
        name: this.state.name,
        setName: this.setName
      }}>
        <h2>Name: {name}</h2>
        <div>
          <NameSwitcher />
        </div>
      </NameContext.Provider>
    );
  }
}

We have the same context object, but the state change functions, and the states are at different places.

We pass them into the value prop in on the object.

Since we passed them into the value prop’s object, we can access them in NameSwitcher .

The only difference is that we need to add a NameContext.Consumer and a callback inside it to access the properties.

They’re in the parameter of the callback.

Difference Between useCallback and useMemo

We use useMemo to memoize a calculation result a function’s calls and renders.

useCallback is for memoizing a callback itself between renders.

The reference is cached.

useRef is to keep data between renders.

useState does the same.

Therefore, we can use useMemo to avoid heavy calculations.

useCallback fixes performance issues where inline handlers cause a PureComponent ‘s child to render again.

This happens because function expressions are different referentially each time they render.

Make Ajax Request in Redux

We can make Ajax requests with Redux if we use the Redux Thunk middleware.

To install it, we run:

npm install redux-thunk

To use it, we write:

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers/index';

const store = createStore(rootReducer, applyMiddleware(thunk));

We import the rootReducer and the thunk middleware.

And we call applyMiddleware to apply the thunk middleware.

Now we can create our own thunk by writing:

const fetchUsers = () => {
  return dispatch => {
     fetch(`https://randomuser.me/api/`)
       .then(res => res.json())
       .then(res => {
          dispatch(saveUserData(res))
       })
   }
}

store
  .dispatch(fetchUsers())
  .then(() => {
    //...
  })

We pass a function that has the dispatch parameter, which is the Redux dispatch function, into the store.dispatch method.

The dispatch parameter is called to put the data into the Redux store.

Then to map this function to our component’s props, we can write:

function mapDispatchToProps(dispatch) {
  return {
    fetchUsers: () => dispatch(fetchUsers()),
  };
}

Now we can call this.prop.fetchUsers to call dispatch our async action.

fetchUsers is an action creator function, but it’s async.

It can be so thanks to the Redux Thunk plugin.

Conclusion

We can use the context API to share data with class and function components.

If we want a more flexible way to share data, we can use Redux with Redux Thunk.

This lets us set data in an async manner.

Categories
React Tips

React Tips — Test Buttons, Form Validation, and 404 Routes

React is a popular library for creating web apps and mobile apps.

In this article, we’ll look at some tips for writing better React apps.

Simulate a Button Click in Jest

To simulate a button click with Jest, we can write:

import React from 'react';
import { shallow } from 'enzyme';
import Button from './Button';

describe('Test button', () => {
  it('Test click event', () => {
    const mockOnClick = jest.fn();
    const button = shallow((<Button onClick={mockOnClick}>click me</Button>));
    button.find('button').simulate('click');
    expect(mockOnClick.mock.calls.length).toEqual(1);
  });
});

We create a mock function with and pass it into our button component.

Then we get the button and call simulate with the argument 'click' do simulate a click.

Then we can check if the mockOnClick function is called at the end.

Alternatively, we can write similar code in a Sinon test.

For instance, we can write:

import React from 'react';
import { shallow } from 'enzyme';
import sinon from 'sinon';

import Button from './Button';

describe('Test Button component', () => {
  it('simulates click events', () => {
    const mockOnClick = sinon.spy();
    const button = shallow((<Button onClick={mockOnClick}>click me</Button>));

    button.find('button').simulate('click');
    expect(mockOnClick).toHaveProperty('callCount', 1);
  });
});

We create a mock function with sinon.spy() .

Then we pass that into the onClick callback.

And then we call simulate with the 'click' argument to simulate the click.

We can also call our onClick function from the props directly.

For example, we can write:

wrapper.find('Button').prop('onClick')()

How to Add Multiple Middlewares to Redux

We can add multiple middlewares to Redux with the applyMiddleware method.

For example, we can write:

const createStoreWithMiddleware = applyMiddleware(ReduxThunk, logger)(createStore);

We just pass them all into the applyMiddleware function directly.

Also, we can put them all into an array and spread them into applyMiddleware :

const middlewares = [ReduxThunk, logger];
applyMiddleware(...middlewares)

Callback When DOM is Loaded in React

componentDidMount is the callback that’s called when the DOM is loaded.

For instance, we can write:

class Comp1 extends React.Component {
  constructor(props) {
    super(props);
    this.handleLoad = this.handleLoad.bind(this);
  }

  componentDidMount() {
    window.addEventListener('load', this.handleLoad);
  }

  componentWillUnmount() {
    window.removeEventListener('load', this.handleLoad)
  }

  handleLoad() {
    //...
  }

  render() {
    //...
  }
}

We have the handleLoad method that we added as the listener fo the load event in componentDidMount .

In componentWillUnmount , we remove the event listener when the component unloads.

Properly Validate Input Values with React

To make form validation easy for us, we can use the react-hook-form library.

For instance, we can write:

import React from 'react'
import useForm from 'react-hook-form'

function App() {
  const { register, handleSubmit, errors } = useForm()
  const onSubmit = (data) => { console.log(data) }

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input name="firstname" ref={register} />

      <input name="lastname" ref={register({ required: true })} />
      {errors.lastname && 'last name is required'} {

      <input name="age" ref={register({ pattern: /d+/ })} />
      {errors.age && 'invalid age.'}

      <input type="submit" />
    </form>
  )
}

We use the useForm hook that comes with the package.

Then we can use the functions and objects that are returned with the hook to check for valid input and display error messages.

register lets us register the input field with react-hook-form and add the validation rules.

Registering the field will make it available when we run our submit handler.

We can pass an object with the validation rules as we did in the last name and age inputs.

We applied the required rule with the last name field.

And we set a regex for the valid format in the age field.

In the onSubmit function, we have the data parameter which has the filled-in values.

It’s only run if all the fields are valid.

errors have errors in a field.

Add a Not Found Page with React Router

We can add a not found page with React Router by adding a Router compoent with no path.

For instance, we can write:

<Switch>
  <Route exact path="/">
    <Home />
  </Route>
  <Route path="/will-match">
    <WillMatch />
  </Route>
  <Route path="*">
     <NoMatch />
  </Route>
</Switch>

The last entry:

<Route path="*">
  <NoMatch />
</Route>

is the wildcard route that’s loaded when the URL doesn’t match anything else listed.

Conclusion

We can create a no matching route with React Router.

We can simulate a button click with a mock function.

To make form validation easy, we should use a package.

And we can add multiple middlewares with React-Redux.