Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecate subscribeToStateUpdates #142

Merged
merged 3 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 33 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,56 +90,51 @@ function SearchBar() {
}
```

## Class Components

For users that want to use class components instead of functional components, you can use the `AnswersHeadlessContext` directly to dispatch actions, and the `subscribeToStateUpdates` HOC to receive updates from state.

These also work with functional components.
## `AnswersHeadlessContext`
### Class Components

## `subscribeToStateUpdates`
For users that want to use class components instead of functional components, you can use the `AnswersHeadlessContext` directly to dispatch actions and receive updates from state.

Here is our MostRecentSearch component again, rewritten as a class with `subscribeToStateUpdates`.
As an example, here is our simple SearchBar again, rewritten as a class using `AnswersHeadlessContext`.

```tsx
import { subscribeToStateUpdates } from '@yext/answers-headless-react';
import { AnswersHeadlessContext, AnswersHeadless, State } from '@yext/answers-headless-react';
import { Component } from 'react';

interface Props {
mostRecentSearch: string
}
export default class Searcher extends Component {
static contextType = AnswersHeadlessContext;
unsubscribeQueryListener: any;
state = { query: "" };

class MostRecentSearch extends Component<Props> {
render() {
return <div>Showing results for {this.props.mostRecentSearch}</div>;
componentDidMount() {
const answers: AnswersHeadless = this.context;
this.unsubscribeQueryListener = answers.addListener({
valueAccessor: (state: State) => state.query.mostRecentSearch,
callback: newPropsFromState => {
this.setState({ query: newPropsFromState })
}
});
}
}

export default subscribeToStateUpdates(state => ({
mostRecentSearch: state.query.mostRecentSearch
}))(MostRecentSearch);
```

## `AnswersHeadlessContext`

And here is our simple SearchBar again, rewritten as a class using `AnswersHeadlessContext`.

```tsx
import { AnswersHeadlessContext, AnswersHeadless } from '@yext/answers-headless-react';
import { Component } from 'react';

export default class Searcher extends Component {
static contextType = AnswersHeadlessContext;
componentWillUnmount() {
this.unsubscribeQueryListener();
}

render() {
const answers: AnswersHeadless = this.context;
return <input
onChange={evt => answers.setQuery(evt.target.value)}
onKeyDown={evt => {
if (evt.key === 'Enter') {
answers.executeUniversalQuery();
}
}}
/>
return (
<div>
<p>Query: {this.state.query}</p>
<input
onChange={evt => answers.setQuery(evt.target.value)}
onKeyDown={evt => {
if (evt.key === 'Enter') {
answers.executeUniversalQuery();
}
}}
/>
</div>
)
}
}
```
Expand Down
8 changes: 4 additions & 4 deletions src/subscribeToStateUpdates.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// TODO(SLAP-1485): find out how to specify generic component props without using `any`
// I sank a 3-4 hours into this but couldn't figure out exactly how to get it to work.
// May require use of typescript generics.

/* eslint-disable @typescript-eslint/no-explicit-any */
import { ComponentType, useReducer, useEffect, useContext } from 'react';
import { State } from '@yext/answers-headless';
Expand All @@ -13,6 +9,10 @@ type SubscriberGenerator = (WrappedComponent: ComponentType<any>) => (props: any
/**
* Generates a HOC that updates a given Component's props based on the current
* answers-headless state and a given mapping function.
*
* @deprecated
* For class component, use `AnswersHeadlessContext` directly to dispatch actions and receive state updates.
* For functional component, use `useAnswersActions` and `useAnswersState` instead.
*/
export function subscribeToStateUpdates(
mapStateToProps: (s: State) => Record<string, unknown>
Expand Down