-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
How to pass parameter to selector? #772
Comments
I found below is work const mapStateToProps = createStructuredSelector({
channelId: selectChannelId(),
channel: selectChannel()
}); What's the difference between the two? const mapStateToProps = (state, ownProps) => {
return {
channelId: ownProps.params.channelId,
channel: selectChannel()
}
}; I must use |
I believe you need to pass the selectors you are composing from as an array, like this: const selectChannel = () => createSelector(
[ selectChannels, selectChannelId ],
(channels, channelId) => channels.find((channel) => channel.channel_id == channelId)
); Check out this link for more info on using |
Check out this link const mySelector = createSelector(
state => state.values.value1,
state => state.values.value2,
(value1, value2) => value1 + value2
)
// You can also pass an array of selectors
const totalSelector = createSelector(
[
state => state.values.value1,
state => state.values.value2
],
(value1, value2) => value1 + value2
) they are the some |
You can also pass parameters right from mapStateToProps: const mapStateToProps = createStructuredSelector({
channel: selectChannelById(id) // Actually, it's not possible. Sorry for confusion 😅
}); Selector: const selectChannelById = (id) => createSelector(
selectChannels(),
(channels) => channels.find((channel) => channel.channel_id == id)
); But these kinds of selectors could be useful in sagas: const channel = yield select(selectChannelById(id)); |
Since this question has been answered quite thoroughly I'll close this issue, but feel free to continue the conversation! |
@igorbarbashin , thanks const mapStateToProps = createStructuredSelector({
channel: selectChannelById(id)
}); but the id is in route params, I can get id using How can I pass it to selectChannelById ? |
@jk2K Yeah, I wrote it and then understood that there's no access to that variable from mapStateToProps. As I then mentioned in the last line this selector could be used in redux-saga. Your solution is what I actually use:
|
@jk2K and @igorbarbashin Selectorsexport const selectFileTypeIsUploading = () => createSelector(
selectYourStateHere(),
state => fileType => state.getIn(['uploading', fileType])
); ComponentMapStateToPropscreateStructuredSelector({
fileTypeIsUploading: selectFileTypeIsUploading()
}); propTypesstatic propTypes = {
fileTypeIsUploading: React.PropTypes.func,
} Usage<FileUpload
title={uploadType}
uploading={this.props.fileTypeIsUploading(uploadType)}
/> |
@arjshiv How to modify your code to support ownProps ? |
@eromoe I think it's like this: const mapStateToProps = createStructuredSelector({
something: (state, ownProps) => selectSomethingById(ownProps.id)(state, ownProps)
}); |
@eromoe I use it this way and it works:
|
@eromoe here is my solution and it works: component/containerstatic propTypes = {
match: PropTypes.object, // route parameter
};
const mapStateToProps = createStructuredSelector({
member: makeSelectMemberById(),
}); selectorconst selectMembers = (state) => state.get('members');
const makeSelectMemberById = () => createSelector(
selectMembers,
// state: redux store
// props: connected component's props
(state, props) => props.match.params.memberId,
(members, memberId) => members.find((member) => member.id === memberId)
); selector will recompute when the |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I want to get channel with channelId prop
My code
selectors
I console the
channel
but i get below, non value, non derived data
Am I doing wrong?
The text was updated successfully, but these errors were encountered: