
Research
/Security News
9 Malicious NuGet Packages Deliver Time-Delayed Destructive Payloads
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.
@neurosnap/react-fetcher
Advanced tools
Make async calls when rendering a component.
Some components need to request data but don't require being stored in redux.
For example, if the data is only being used for that one component, it is not
necessary to place it into redux. However, leveraging lifecycle methods like
componentDidMount and setState has its own set of problems because of the way react handles
mounting. For example, an unmounted component cannot call setState so if an
async request comes back when the component gets unmounted then that will result in
an error. People get around that issue by adding a class property this._mounted
and then setting it to false when unmounting. This adds quite a bit of complexity
to a component and some argue is an anti-pattern.
There are also cases where the component will not unmount when you expect it to
and the async function will not be activated.
This library attempts to create a simple API for calling an async
function and returning some data to the component. This separates data fetching
from the actual component and uses roughly the same API that react-redux and
redux-saga uses to keep things easy to test and migrate to a redux container component.
redux for easy migrationredux and redux-sagayarn add @neurosnap/react-fetcher
import createFetcher from 'react-fetcher';
const fetch = window.fetch;
async function fetchMovies() {
const resp = await fetch('http://httpbin.org/get?movies=one,two,three');
const json = await resp.json();
const movies = json.args.movies.split(',');
return movies;
}
const DisplayMovies = ({ data = [] }) => (
<div>
{data.map((movie) => <div key={movie}>{movie}</div>)}
</div>
);
const movieFetcher = createFetcher(fetchMovies);
const DisplayMoviesContainer = movieFetcher()(DisplayMovies);
const App = () => (
<div>
<DisplayMoviesContainer />
</div>
);
The async function could be anything that co supports.
A generator, a promise, an array of promises, an object of promises, even a normal function.
react-fetcher will take the result of what you pass it and send it to the component.
Here at ReactFetcher, Inc. we like to use cosed
which treats side effects as data with an API similar to redux-saga:
import createFetcher from 'react-fetcher';
import { call } from 'cosed';
const fetch = window.fetch;
function* fetchMovies() {
const resp = yield call(fetch, 'http://httpbin.org/get?movies=one,two,three');
const json = yield call([resp, 'json']);
const movies = json.args.movies.split(',');
return movies;
}
Using cosed makes testing side effects simple.
Want to change the way the data gets sent to the component? Use mapStateToProps
import createFetcher from 'react-fetcher';
const fetch = window.fetch;
async function fetchMovies() {
const resp = await fetch('http://httpbin.org/get?movies=one,two,three');
const json = await resp.json();
const movies = json.args.movies.split(',');
return movies;
}
const DisplayMovies = ({ data = [] }) => (
<div>
{movies.map((movie) => <div key={movie}>{movie}</div>)}
</div>
);
const movieFetcher = createFetcher(fetchMovies);
const mapStateToProps = (movies) => ({ movies }); // default mapStateToProps: (data, error) => ({ data, error });
const DisplayMoviesContainer = movieFetcher(mapStateToProps)(DisplayMovies);
const App = () => (
<div>
<DisplayMoviesContainer />
</div>
);
Want to refetch data? Like mapDispatchToProps in redux, we have mapRefetchToProps
which will bust the cache and call the request again.
const DisplayMovies = ({ movies = [], refetch }) => {
return h('div', [
h('a', { href: '#', onClick: () => { refetch() } }, 'refetch'),
h('div', movies.map((movie) => h('div', { key: movie }, movie))),
]);
};
const movieFetcher = createFetcher(fetchMovies);
const mapStateToProps = (movies) => ({ movies });
const mapRefetchToProps = (refetch) => ({ refetch });
const DisplayMoviesContainer = movieFetcher(
mapStateToProps,
mapRefetchToProps,
)(DisplayMovies);
Async function also receives props sent to component
function* fetchMovies({ movieName }) {
const resp = yield call(fetch, `http://httpbin.org/get?movies=${movieName}`);
const json = yield call([resp, 'json']);
const movies = json.args.movies.split(',');
return movies;
}
const DisplayMovies = ({ movies = [] }) => (
<div>
{movies.map((movie) => <div key={movie}>{movie}</div>)}
</div>
);
const movieFetcher = createFetcher(fetchMovies);
const mapStateToProps = (movies) => ({ movies });
const DisplayMoviesContainer = movieFetcher(mapStateToProps)(DisplayMovies);
const App = () => (
<div>
<DisplayMoviesContainer movieName="Transporter" />
</div>
);
Async function returns an error? mapStateToProps has a second parameter for
any error states that are returned from the async function
function* fetchMovies({ movieName }) {
throw new Error('Something bad happened');
}
const DisplayMovies = ({ movies = [], error }) => {
if (error) {
return <div>{error.message}</div>;
}
return (
<div>
{movies.map((movie) => <div key={movie}>{movie}</div>)}
</div>
);
};
const movieFetcher = createFetcher(fetchMovies);
const mapStateToProps = (movies, error) => ({
movies: movies || [],
error,
});
const DisplayMoviesContainer = movieFetcher(mapStateToProps)(DisplayMovies);
const App = () => (
<div>
<DisplayMoviesContainer movieName="Transporter" />
</div>
);
Want a loader?
const Loader = () => <div>LOADING!</div>;
const DisplayMoviesContainer = movieFetcher(mapStateToProps)(DisplayMovies, Loader);
FAQs
Make async calls when rendering a component.
We found that @neurosnap/react-fetcher demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Research
/Security News
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.