I've got a React application and I'm trying to use Reux-ORM and I'm struggling with selectors. I've got a simple app with normalized data
// /models/team.js
import { Model, many, attr } from 'redux-orm';
import Player from './player';
class Team extends Model {
static reducer(action, Team, session) {
[...]
}
}
Team.modelName = "Team";
Team.fields = {
id: attr(),
players: many('Player;),
}
and
// /models/player.js
import { Model, attr } from 'redux-orm';
class Player extends Model {
static reducer(action, Player, session) {
[...]
}
}
Player.modelName = "Player";
Player.fields = {
id: attr(),
}
I register them into the ORM as follows:
// /selectors/selector.js
import { ORM } from 'redux-orm';
import Team from '../models/team';
import Player from '../models/player';
const orm = new ORM();
orm.register(Team, Player);
export default orm;
So far so good. Now I would like to have a React Component that renders a list of all teams with all players. So the first thing I did was to write a selector:
// /selectors/selector.js
import { createSelector } from "redux-orm";
import orm from "../models/index";
export const teams = createSelector(
orm,
state => state.orm,
session => {
return session.Team.all().toRefArray();
}
);
This gives a list of teams, but not yet its associated players. So in my component, I can now use teams
from my props with if I add a function const mapStateToProps = state => ({teams: teams(state)});
But now my question is, what's the best way to get the team-specific players? Do I add them to the returned teams in the selector? Or do I write a separate selector? And would I pass on the players to the the component directly, or rather make a separate component in the Team component?
class Teams extends React.Components {
render() {
return this.props.teams.map(team => {
/* Option 1: */
const teamPlayer = team.players // returned by initial selector
/* Option 2: */
const teamPlayers = [some call to selector based on 'team'];
/* Option 3: */
// Don't pass on the players but the id instead
return (
<Team
/* Option 1 & 2: */
players= {teamPlayers}
/* Option 3: */
teamId = {team.id} // and within the Team component make a selector call
/>
}
}
Implementationally, I got stuck at making the selectors and couldn't find examples for this case for redux-orm v0.19
, and conceptually I'm still not sure which way is the best to go. All help is much appreciated!