tonyhb / autoaction Goto Github PK
View Code? Open in Web Editor NEWDeclarative data loading and action calling within react-redux
Declarative data loading and action calling within react-redux
The deep-equal dependency is missing when the package is installed from NPM. IT should be listed in package.json under dependencies.
What should AutoAction really do?
It should:
It should not:
When defining a decorator in the form:
const actionCreators = {
load: loadSpy
};
const autoActions = {
load: {
args: (state, props) => "xyz",
key: (state, props) => "xyz"
}
};
const decorator = autoaction(autoActions, actionCreators);
It fails to work(throwwing an exception) properly because of the fact the check for args being a function is incorrectly made within the autoaction lib. A folllow up PR fixing this will follow shortly.
PTAL at how to do this.
If any two components in a tree call an action using props as an argument, and those props were passed down from an initial action call, we should batch and dedupe these actions.
autoaction
sounds very useful, however I think I'm misunderstanding how to use it.
I tried to create an example where I have a Dashboard
which has many Cards
, some of which might be duplicated, and I only want to fetch each Card
once.
It works, but the card requests aren't deduplicated. The logs for I get for dashboard 100 which has cards 101, 102, 102, 103 are:
GET_DASHBOARD 100
GET_CARD 101
GET_CARD 102
GET_CARD 102 <-- duplicate
GET_CARD 103
Also, I (perhaps naively) assumed if a prop changes that results in an action argument changing then it will fire the action again with the new arguments, but that doesn't appear to be the case.
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import { Provider, connect } from 'react-redux'
import { createStore } from 'redux'
import autoaction from 'autoaction';
import { createStructuredSelector } from 'reselect';
const actions = {
getDashboardById(id) {
console.log("GET_DASHBOARD", id)
return {
type: "GET_DASHBOARD",
payload: {
id: id,
name: "dashboard"+id,
cards: [id + 1, id + 2, id + 2, id + 3]
}
};
},
getCardById(id) {
console.log("GET_CARD", id)
return {
type: "GET_CARD",
payload: {
id: id,
name: "card"+id
}
};
}
}
function reducer(state = { cards: {}, dashboards: {} }, { type, payload }) {
switch (type) {
case "GET_DASHBOARD":
return { ...state, dashboards: { ...state.dashboards, [payload.id]: payload }};
case "GET_CARD":
return { ...state, cards: { ...state.cards, [payload.id]: payload }};
default:
return state;
}
}
@connect(createStructuredSelector({
dashboard: (state, props) => state.dashboards[props.dashboardId]
}))
@autoaction({
getDashboardById: (props, state) => props.dashboardId
}, actions)
class Dashboard extends Component {
render() {
const { dashboard } = this.props;
return (
<div>
<h1>dashboard={dashboard && dashboard.name}</h1>
<ul>
{dashboard && dashboard.cards.map(id =>
<Card cardId={id} />
)}
</ul>
</div>
);
}
}
@connect(createStructuredSelector({
card: (state, props) => state.cards[props.cardId]
}))
@autoaction({
getCardById: (props, state) => props.cardId
}, actions)
class Card extends Component {
render() {
const { card } = this.props;
return (
<li>card={card && card.name}</li>
);
}
}
class App extends Component {
constructor(props, context) {
super(props, context);
this.state = {
dashboardId: "100"
}
}
render() {
const { dashboardId } = this.state;
return (
<div className="App">
<input value={dashboardId} onChange={(e) => this.setState({ dashboardId: e.target.value })} />
<Dashboard dashboardId={parseInt(dashboardId)} />
</div>
);
}
}
ReactDOM.render(
<Provider store={createStore(reducer)}>
<App />
</Provider>,
document.getElementById('root')
);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.