0.0.1-7 • Published 2 months ago

react-hook-use-cta v0.0.1-7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

react-hook-use-cta: useCTA (use Call To Action)

A somewhat flexible react hook alternative to React.useReducer. Written in Typescript.

Demo Playground

Table of Contents


Installation

react-hook-use-cta fast-equals

NPM

npm i react-hook-use-cta fast-equals

export { useCTA }

https://github.com/rafde/react-hook-use-cta/blob/9e9206f1ff06e2de5adcde5d107d9d847e210063/src/index.ts#L9-L14

Usage

import { useEffect, } from "react";
import { useCTA, } from 'react-hook-use-cta'

function View() {
	const [
		state,
		dispatch,
	] = useCTA({
		initial: {
			search: 'initial',
			isFuzzy: false, 
			count: 0,
		}
	});

	useEffect(
		() => dispatch.cta.update('search', 'update'),
		[]
	);

	/* Renders `update` */
	return <>{state.search}</>
}
import { useEffect, } from "react";
import { useCTA, } from 'react-hook-use-cta'

function View(props: { initial: { search: string, isFuzzy: boolean, count: 0 } }) {
	const [
		state,
		dispatch,
	] = useCTA({
		initial: props.initial,
		onInit(initial) {
			return {
				...initial,
				search: 'onInit',
			}
		},
		actions: {
			// START: augment predefined actions
			replace(ctaParam, payload) {
				return payload;
			},
			replaceInitial(ctaParam, payload) {
				return payload;
			},
			reset(ctaParam, payload) {
				return payload;
			},
			update(ctaParam, payload) {
				return payload;
			},
			// END: augment predefined actions

			// START: Custom actions
			toggleIsFuzzy(ctaParam, isFuzzy?: boolean) {
				if (typeof isFuzzy === 'undefined') {
					return {
						isFuzzy: !ctaParam.previous.isFuzzy,
					}
				}

				return {
					isFuzzy
				}
			},
			addToCount(ctaParam, value: number) {
				return {
					count: ctaParam.previous.count + value,
				}
			},
			incrementCount(ctaParam) {
				return {
					count: ctaParam.previous.count + 1,
				}
			},
			// END: Custom actions
		}
	});

	useEffect(
		() => dispatch.cta.update('search', 'update'),
		[]
	);

	return <>
		<div>{state.search}</div>
		<div>{dispatch.state.initial.search}</div>
		<div>{dispatch.state.changes?.search}</div>
		<div>{dispatch.state.previous.search}</div>
	</>
}

Parameter

Required

Key/value object of type UseCTAParameter

https://github.com/rafde/react-hook-use-cta/blob/9e9206f1ff06e2de5adcde5d107d9d847e210063/src/types/UseCTAParameter.ts#L4-L11

Typescript Definition:


Parameter: initial

Required

https://github.com/rafde/react-hook-use-cta/blob/1e97f6ef6c5bc3d053cf64007bfea7c5c84877a0/src/types/UseCTAParameter.ts#L9

Similar to React.useReducer initialArg parameter, but it only takes key/value object that defines the shape of your state.

Values can be anything that strictDeepEqual from fast-equals supports.

Typescript Definition:


Parameter: onInit

Optional

https://github.com/rafde/react-hook-use-cta/blob/1e97f6ef6c5bc3d053cf64007bfea7c5c84877a0/src/types/UseCTAParameter.ts#L10

Similar to React.useReducer init parameter. Called on first time render. A function that is called to replace initial value.

Typescript Definition:


Parameter: actions

Optional

Read once on first time render. Key/value object to define the types of actions to implement.

The following results will not trigger re-render for all actions:

Predefine actions

There are predefined actions that can be augmented with the following signatures:

https://github.com/rafde/react-hook-use-cta/blob/7532ce5d41502f3fc9a0b0275ba2eaae7c2c16fe/src/types/UseCTAParameterActionsPredefinedRecord.ts#L4-L9

Augmenting these actions will affect custom actions.

Typescript Definition:

Predefined calls to action:


Parameter: actions?.['customAction']

You can define your own custom actions to handle payloads to your specifications.

Typescript signature:

https://github.com/rafde/react-hook-use-cta/blob/adfd2a0448f08a4d1374db0136f77dde9c64da7a/src/types/UseCTAParameterActionsRecordProp.ts#L6-L14

Typescript Definitions:

Call to action:

import { useCTA } from 'react-hook-use-cta'

function View(props: {initial: {search: string, isFuzzy: boolean, count: 0}}) {
	const [
		state,
		dispatch,
	] = useCTA({
		initial: props.initial,
		onInit(initial) {
			return {
				...initial,
				search: 'onInit',
			}
		},
		actions: {
			// START: Custom actions
			toggleIsFuzzy(ctaParam, payload?: boolean) {
				if (typeof payload === 'undefined') {
					return {
						isFuzzy: !ctaParam.previous.isFuzzy,
					}
				}
				
				return {
					isFuzzy: payload,
				}
			},
			addToCount(ctaParam, payload: number) {
				const count = ctaParam.previous.count + payload;
				if (isNaN(count) || count < 0) {
					return;
				}
				return {
					count,
				}
			},
			incrementCount(ctaParam) {
				return {
					count: ctaParam.previous.count + 1,
				}
			},
			// END: Custom actions
		}
	});

	useEffect(
		() => dispatch.cta.addToCount(4),
		[]
	);

	return <>{state.count}</>
}

actions?.['customAction'] 1st Parameter: CustomCTAParam

Custom actions' first parameter:

https://github.com/rafde/react-hook-use-cta/blob/bf4d06d68f391b8ed1a6a641c051338d4de1e70d/src/types/CustomCTAParam.ts#L11-L16

extends UseCTAReturnTypeDispatchState with 4 additional functions that affect the behavior of the action.

https://github.com/rafde/react-hook-use-cta/blob/bf4d06d68f391b8ed1a6a641c051338d4de1e70d/src/types/CustomCTAParam.ts#L12-L15

Accepts result and options

https://github.com/rafde/react-hook-use-cta/blob/bf4d06d68f391b8ed1a6a641c051338d4de1e70d/src/internal/ActionTypes.ts#L8

If predefined actions were augmented, {useCustom: false} will bypass them and use default predefined behavior.


actions?.['customAction'] 1st Parameter: CustomCTAParam.replaceAction

https://github.com/rafde/react-hook-use-cta/blob/bf4d06d68f391b8ed1a6a641c051338d4de1e70d/src/types/CustomCTAParam.ts#L12

Returns instance of ReplaceActionType

https://github.com/rafde/react-hook-use-cta/blob/bf4d06d68f391b8ed1a6a641c051338d4de1e70d/src/internal/ActionTypes.ts#L26

Returning ReplaceActionType will produce the same outcome as dispatch.cta.replace

const [state, dispatch] = useCTA({
	///...parameter
	actions: {
		specialReplace(ctaParam, payload?: boolean) {
			if (typeof payload === 'undefined') {
				// replace `state` with this new state
				return ctaParam.replaceAction({
					...ctaParam.previous,
					isFuzzy: true,
				})
			}
			
			// update state
			return {
				isFuzzy: payload,
			}
		},
	},
});

actions?.['customAction'] 1st Parameter: CustomCTAParam.replaceInitialAction

https://github.com/rafde/react-hook-use-cta/blob/bf4d06d68f391b8ed1a6a641c051338d4de1e70d/src/types/CustomCTAParam.ts#L13

Returns instance of ReplaceInitialActionType

https://github.com/rafde/react-hook-use-cta/blob/bf4d06d68f391b8ed1a6a641c051338d4de1e70d/src/internal/ActionTypes.ts#L43

Returning ReplaceIntialActionType will produce the same outcome as dispatch.cta.replaceInitial

const [state, dispatch] = useCTA({
	///...parameter
	actions: {
		specialReplaceInitial(ctaParam, payload?: boolean) {
			if (typeof payload === 'undefined') {
				// replace `initial` with this new state
				return ctaParam.replaceInitialAction({
					...ctaParam.previous,
					isFuzzy: true,
				})
			}

			// update state
			return {
				isFuzzy: payload,
			}
		},
	},
});

actions?.['customAction'] 1st Parameter: CustomCTAParam.resetAction

https://github.com/rafde/react-hook-use-cta/blob/bf4d06d68f391b8ed1a6a641c051338d4de1e70d/src/types/CustomCTAParam.ts#L14

Returns instance of ResetActionType

https://github.com/rafde/react-hook-use-cta/blob/bf4d06d68f391b8ed1a6a641c051338d4de1e70d/src/internal/ActionTypes.ts#L60

Returning ResetActionType will produce the same outcome as dispatch.cta.reset with payload parameter

const [state, dispatch] = useCTA({
	///...parameter
	actions: {
		specialReplaceInitial(ctaParam, payload?: boolean) {
			if (typeof payload === 'undefined') {
				// replace `initial` and `state` with this new state
				return ctaParam.resetAction({
					...ctaParam.previous,
					isFuzzy: true,
				})
			}

			// update state
			return {
				isFuzzy: payload,
			}
		},
	},
});

actions?.['customAction'] 1st Parameter: CustomCTAParam.updateAction

https://github.com/rafde/react-hook-use-cta/blob/bf4d06d68f391b8ed1a6a641c051338d4de1e70d/src/types/CustomCTAParam.ts#L15

Returns instance of UpdateActionType

https://github.com/rafde/react-hook-use-cta/blob/bf4d06d68f391b8ed1a6a641c051338d4de1e70d/src/internal/ActionTypes.ts#L77

Returning UpdateActionType will produce the same outcome as dispatch.cta.update

const [state, dispatch] = useCTA({
	///...parameter
	actions: {
		specialReplaceInitial(ctaParam, payload?: boolean) {
			if (typeof payload === 'undefined') {
				// replace `initial` and `state` with this new state
				return ctaParam.updateAction({
					isFuzzy: true,
				})
			}

			// update state
			return ctaParam.previous
		},
	},
});

actions?.['customAction'] 2nd Parameter: payload

payloads can be:

  • undefined
  • optional or required where the value can be anything
  • Initial type
  • Partial<Initial> type

actions?.['customAction'] Return Type

Return type can be


Return Type

Array with two values: [state, dispatch] of type UseCTAReturnType

https://github.com/rafde/react-hook-use-cta/blob/bf4d06d68f391b8ed1a6a641c051338d4de1e70d/src/types/UseCTAReturnType.ts#L4-L10

Typescript Definitions:


Return Type: state

Key/value object of type CTAInitial

The current state. During the first render, it’s set to the result of onInit or initial if onInit was not defined.


Return Type: dispatch

Is type UseCTAReturnTypeDispatch

https://github.com/rafde/react-hook-use-cta/blob/0ed13652508f2b3afb74cd7d35c920a3291b5620/src/types/UseCTAReturnTypeDispatch.ts#L219-L225

Typescript Definition:

A function with static read-only properties dispatch.state and dispatch.cta.

Triggers re-render when state or dispatch.state.initial changes

Parameters description will be covered by:


Return Type: dispatch.cta

https://github.com/rafde/react-hook-use-cta/blob/0ed13652508f2b3afb74cd7d35c920a3291b5620/src/types/UseCTAReturnTypeDispatch.ts#L223

Has predefined actions

https://github.com/rafde/react-hook-use-cta/blob/0ed13652508f2b3afb74cd7d35c920a3291b5620/src/types/UseCTAReturnTypeDispatch.ts#L192-L198

and custom actions if defined in actions?.['customAction'].

Typescript Definition:


Return Type: dispatch.cta?.['customAction']

Optional

Custom call to action for changing state

Parameters are based on the expected payload you defined them in Parameter: actions?.['customAction']


Return Type: dispatch.cta?.['customAction'] with payload parameter
dispatch.cta.addToCount(5);

// Alias for
dispatch({
	type: 'addToCount',
	payload: 5
});

// or
dispatch.cta.addToCount((ctaParam) => {
	return ctaParam.previous.count + 10;
});

// Alias for
dispatch({
	type: 'addToCount',
	payload: (ctaParam) => {
		return ctaParam.previous.count + 10;
	}
});

// or
dispatch.cta.addToCount((ctaParam) => {
	// No re-render
	return;
});

// Alias for
dispatch({
	type: 'addToCount',
	payload: (ctaParam) => {
		// No re-render
		return;
	}
});

Return Type: dispatch.cta?.['customAction'] without parameter
dispatch.cta.incrementCount();

// Alias for
dispatch({
	type: 'incrementCount',
});

Effects


Return Type: dispatch.cta.update

Overloaded function for partially changing state

https://github.com/rafde/react-hook-use-cta/blob/0ed13652508f2b3afb74cd7d35c920a3291b5620/src/types/UseCTAReturnTypeDispatch.ts#L196-L197

Typescript Definition:


Return Type: dispatch.cta.update with payload parameter

Accepts partial key/values from Initial and updates state with partial change(s)

https://github.com/rafde/react-hook-use-cta/blob/0ed13652508f2b3afb74cd7d35c920a3291b5620/src/types/UseCTAReturnTypeDispatch.ts#L196

dispatch.cta.update({
	// partial `state` change
	search: 'update',
	count: 8,
});

// Alias for
dispatch({
	type: 'update',
	payload: {
		// partial `state` change
		search: 'update',
		count: 8,
	}
});

// or
dispatch.cta.update((ctaParam) => {
	return {
		// partial `state` change
		search: 'update',
		count: 8,
	};
});

// Alias for
dispatch({
	type: 'update',
	payload: (ctaParam) => {
		return {
			// partial `state` change
			search: 'update',
			count: 8,
		};
	}
});

// or
dispatch.cta.update((ctaParam) => {
	// No re-render
	return;
});

// Alias for
dispatch({
	type: 'update',
	payload: (ctaParam) => {
		// No re-render
		return;
	}
});

Return Type: dispatch.cta.update with key and value parameters

https://github.com/rafde/react-hook-use-cta/blob/1e97f6ef6c5bc3d053cf64007bfea7c5c84877a0/src/types/UseCTAReturnTypeDispatch.ts#L197

Accepts a key from Initial and a corresponding value type for that key from Initial[keyof Initial] and updates state with partial change

dispatch.cta.update('seatch', 'update'); // partial `state` change

// Alias for
dispatch.cta.update({
	seatch: 'update',
});

Effects


Return Type: dispatch.cta.replace

Replace entire state with a new state.

https://github.com/rafde/react-hook-use-cta/blob/0ed13652508f2b3afb74cd7d35c920a3291b5620/src/types/UseCTAReturnTypeDispatch.ts#L193

Typescript Definition:

dispatch.cta.replace({
	// new `state`
	search: 'replace',
	isFuzzy: false,
	count: 8,
});

// Alias for
dispatch({
	type: 'replace',
	payload: {
		// new `state`
		search: 'replace',
		isFuzzy: false,
		count: 8,
	}
});

// or
dispatch.cta.replace((ctaParam) => {
	return {
		// new `state`
		search: 'replace',
		isFuzzy: false,
		count: 8,
	};
});

// Alias for
dispatch({
	type: 'replace',
	payload: (ctaParam) => {
		return {
			// new `state`
			search: 'replace',
			isFuzzy: false,
			count: 8,
		};
	}
});

// or
dispatch.cta.replace((ctaParam) => {
	// No re-render
	return;
});

// Alias for
dispatch({
	type: 'replace',
	payload: (ctaParam) => {
		// No re-render
		return;
	}
});

Effects


Return Type: dispatch.cta.replaceInitial

Replace entire dispatch.state.initial value with a new initial value.

https://github.com/rafde/react-hook-use-cta/blob/0ed13652508f2b3afb74cd7d35c920a3291b5620/src/types/UseCTAReturnTypeDispatch.ts#L194

Typescript Definition:

dispatch.cta.replaceInitial({
	// new `initial`
	search: 'replaceInitial',
	isFuzzy: true,
	count: 5,
});

// Alias for
dispatch({
	type: 'replaceInitial',
	payload: {
		// new `initial`
		search: 'replaceInitial',
		isFuzzy: true,
		count: 5,
	}
});

// or
dispatch.cta.replaceInitial((ctaParam) => {
	return {
		// new `initial`
		search: 'replaceInitial',
		isFuzzy: true,
		count: 5,
	};
});

// Alias for
dispatch({
	type: 'replaceInitial',
	payload: (ctaParam) => {
		return {
			// new `initial`
			search: 'replaceInitial',
			isFuzzy: true,
			count: 5,
		};
	}
});

// or
dispatch.cta.replaceInitial((ctaParam) => {
	// No re-render
	return;
});

// Alias for
dispatch({
	type: 'replaceInitial',
	payload: (ctaParam) => {
		// No re-render
		return;
	}
});

Effects


Return Type: dispatch.cta.reset

Overloaded function that differs in behavior when called with payload parameter and without parameter.

https://github.com/rafde/react-hook-use-cta/blob/0ed13652508f2b3afb74cd7d35c920a3291b5620/src/types/UseCTAReturnTypeDispatch.ts#L195

Typescript Definition:


Return Type: dispatch.cta.reset without parameter

Sets state equal to dispatch.state.initial

dispatch.cta.reset();

// Alias for
dispatch({
	type: 'reset',
});

Effects


Return Type: dispatch.cta.reset with payload parameter

Sets state and dispatch.state.initial equal to payload

dispatch.cta.reset({
	// new `state` and `initial`
	search: 'reset',
	isFuzzy: true,
	count: 10,
});

// Alias for
dispatch({
	type: 'reset',
	payload: {
		// new `state` and `initial`
		search: 'reset',
		isFuzzy: true,
		count: 10,
	}
});

// or
dispatch.cta.reset((ctaParam) => {
	return {
		// new `state` and `initial`
		search: 'reset',
		isFuzzy: true,
		count: 10,
	}; 
});

// Alias for
dispatch({
	type: 'reset',
	payload: (ctaParam) => {
		return {
			// new `state` and `initial`
			search: 'reset',
			isFuzzy: true,
			count: 10,
		};
	}
});

// or
dispatch.cta.reset((ctaParam) => {
	// No re-render
	return;
});

// Alias for
dispatch({
	type: 'reset',
	payload: (ctaParam) => {
		// No re-render
		return;
	}
});

Effects


Return Type: dispatch.state

https://github.com/rafde/react-hook-use-cta/blob/0ed13652508f2b3afb74cd7d35c920a3291b5620/src/types/UseCTAReturnTypeDispatch.ts#L224

Is of type UseCTAReturnTypeDispatchState.

https://github.com/rafde/react-hook-use-cta/blob/0ed13652508f2b3afb74cd7d35c920a3291b5620/src/types/UseCTAReturnTypeDispatch.ts#L212-L217

This is extra data information that can be referenced for certain changes over time.

Typescript Definition:


Return Type: dispatch.state.current

https://github.com/rafde/react-hook-use-cta/blob/0ed13652508f2b3afb74cd7d35c920a3291b5620/src/types/UseCTAReturnTypeDispatch.ts#L214

The value is equal to Return Type: state

Typescript Definition:


Return Type: dispatch.state.initial

https://github.com/rafde/react-hook-use-cta/blob/0ed13652508f2b3afb74cd7d35c920a3291b5620/src/types/UseCTAReturnTypeDispatch.ts#L215

Starts of equal to initial or the result of onInit.

It can be changed with dispatch.cta.replaceInitial or dispatch.cta.reset with payload parameter

Typescript Definition:


Return Type: dispatch.state.changes

https://github.com/rafde/react-hook-use-cta/blob/0ed13652508f2b3afb74cd7d35c920a3291b5620/src/types/UseCTAReturnTypeDispatch.ts#L213

  • Partial<Initial>: When key/values of dispatch.state.current are not equal to dispatch.state.initial Example:

    	```ts
    		dispatch.state.initial /* = {
    			search: '',
    			isFuzzy: true,
    			count: 1,
    		} */
    
    		dispatch.state.current /* = {
    			search: 'current',
    			isFuzzy: true,
    			count: 1,
    		} */
    
    		dispatch.state.changes /* = {
    			search: 'current',
    		} */
    
    		if ( dispatch.state.changes ) {
    			console.log('state has changes')
    		}
    	```
  • null: When the key/values dispatch.state.initial and dispatch.state.current are equal. Example:

    	```ts
    		dispatch.state.initial /* = {
    			search: 'current',
    			isFuzzy: true,
    			count: 1,
    		} */
    
    		dispatch.state.current /* = {
    			search: 'current',
    			isFuzzy: true,
    			count: 1,
    		} */
    
    		dispatch.state.changes /* = null */
    
    		if ( !dispatch.state.changes ) {
    			console.log('No changes to state')
    		}
    	```

Typescript Definition:


Return Type: dispatch.state.previous

https://github.com/rafde/react-hook-use-cta/blob/0ed13652508f2b3afb74cd7d35c920a3291b5620/src/types/UseCTAReturnTypeDispatch.ts#L216

Equal to the previous dispatch.state.current value.

Typescript Definition:


Typescript exports

https://github.com/rafde/react-hook-use-cta/blob/bf4d06d68f391b8ed1a6a641c051338d4de1e70d/src/index.ts#L22-L32


export type { CTAInitial, }

https://github.com/rafde/react-hook-use-cta/blob/9e9206f1ff06e2de5adcde5d107d9d847e210063/src/types/CTAInitial.ts#L1


export type { CustomCTAParam, }

https://github.com/rafde/react-hook-use-cta/blob/adfd2a0448f08a4d1374db0136f77dde9c64da7a/src/types/CustomCTAParam.ts#L11-L16


export type { UseCTAParameter, }

See Parameter


export type { UseCTAReturnTypeDispatchState, }

See Return Type: dispatch

export type { UseCTAReturnTypeDispatch, }

See Return Type: dispatch


export type { UseCTAReturnType, }

See Return Type


0.0.1-3

2 months ago

0.0.1-7

2 months ago

0.0.1-5

2 months ago

0.0.1-2

2 months ago