Skip to content

Instantly share code, notes, and snippets.

View chadmuro's full-sized avatar

Chad Murobayashi chadmuro

View GitHub Profile
@chadmuro
chadmuro / Form.js
Last active November 27, 2022 04:19
Signup Page with RHF and MUI
import React from 'react';
import { makeStyles } from '@material-ui/core';
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
import { useForm, Controller } from 'react-hook-form';
const useStyles = makeStyles(theme => ({
root: {
display: 'flex',
flexDirection: 'column',
@chadmuro
chadmuro / test-utils.js
Created April 13, 2021 08:06
Custom Render RTL
import React from "react";
import { render } from "@testing-library/react";
import { Provider } from "react-redux";
import { applyMiddleware, createStore } from "redux";
import { Router } from "react-router-dom";
import thunk from "redux-thunk";
import { createMemoryHistory } from "history";
import reducer from "../store/reducers/index";
const customRender = (ui { actions = [], route = "/" } = {}) => {
@chadmuro
chadmuro / App.test.js
Created April 6, 2021 13:49
Jest and RTL
import { render, screen } from '@testing-library/react';
import App from './App';
test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});
@chadmuro
chadmuro / setupTests.js
Created April 6, 2021 13:48
Jest and RTL
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';
@chadmuro
chadmuro / Form.js
Last active October 12, 2021 02:51
Signup Modal
import React, { useState } from 'react';
import { makeStyles } from '@material-ui/core';
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
const useStyles = makeStyles(theme => ({
root: {
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
@chadmuro
chadmuro / App.js
Created April 4, 2021 08:40
Signup Modal
import { useState } from 'react';
import Button from '@material-ui/core/Button';
import ModalDialog from './ModalDialog';
const App = () => {
// declare a new state variable for modal open
const [open, setOpen] = useState(false);
// function to handle modal open
const handleOpen = () => {
@chadmuro
chadmuro / ModalDialog.js
Created April 4, 2021 08:37
Signup Modal
import React from 'react';
import Dialog from '@material-ui/core/Dialog';
import Form from './Form';
const ModalDialog = ({ open, handleClose }) => {
return (
// props received from App.js
<Dialog open={open} onClose={handleClose}>
// form to be created
<Form handleClose={handleClose} />
@chadmuro
chadmuro / App.js
Created March 24, 2021 14:20
react-error-boundary final
import React, { useState } from 'react';
import Counter from './Counter';
import { ErrorBoundary } from 'react-error-boundary';
const ErrorFallback = ({ error, resetErrorBoundary }) => {
return (
<div role="alert">
<p>Something went wrong:</p>
<pre>{error.message}</pre>
<button onClick={resetErrorBoundary}>Try again</button>
@chadmuro
chadmuro / ErrorBoundary.js
Created March 24, 2021 14:08
react-error-boundary class component
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}
@chadmuro
chadmuro / App.js
Created March 24, 2021 14:00
react-error-boundary example
import React, { useState } from 'react';
import Counter from './Counter';
const App = () => {
const [count, setCount] = useState(0);
return (
<div className="App">
<Counter count={count} setCount={setCount} />
</div>