我的商店中有一个切片是我使用Redux Toolkit中的createEntityAdapter创建的。在函数组件中,您可以将useSelector与从 返回的选择器一起使用。但是如何在基于类的组件中以数组形式获取所有实体(如 selectAll 选择器)?我正在使用connect与mapStateToProps,它只允许我获取实体或 ids 作为道具,但并非所有实体都以与 ids 排序顺序相同的顺序排序......adapter.getSelectors()
react-native redux react-redux redux-toolkit mapstatetoprops
我学习了 JavaScript 和 React Redux 并有这个问题:。
假设我有这个 Redux dispatch:
dispatch(setLogMessage( () =>{"Connecting to env1", 12340, "connecting"}));
Run Code Online (Sandbox Code Playgroud)
减速器在这里是为了上面setLogMessage的:
const initialState = {
log: "",
};
case booksActionTypes.LOG_MESSAGE: {
return {
...state,
log: action.payload,
};
}
Run Code Online (Sandbox Code Playgroud)
在mapStateToProps它看起来像这样:
const [theLog, addLog] = useState([]);
useEffect(() => {
if (props.log !== "") {
addLog([...theLog, createData(props.log)]);
}
}, [props.log]);
function createData(message, timestamp, type) {
console.log('s');
return { message, timestamp, type };
}
Run Code Online (Sandbox Code Playgroud)
问题是我玩弄并想在这种情况下学习箭头函数,并希望拥有上述props.log功能,成为解决上述问题() =>{"Connecting to env1", 12340, "connecting"}) …
我收到一个来自 的 url 参数useParams。我想使用将它传递给选择器mapStateToProps。
集合.组件.jsx
import { useParams } from "react-router-dom";
import { connect } from "react-redux";
import { selectShopCollection } from "../../redux/shop/shop.selectors";
import './collection.styles.scss'
const Collection = ({ collection }) => {
const { collectionId } = useParams();
console.log(collection)
return (
<div>
<h1>{collection}</h1>
</div>
)
}
const mapStateToProps = (state, ownProps) => ({
collection: selectShopCollection(ownProps.match.params.collectionId)(state)
})
export default connect(mapStateToProps)(Collection);
Run Code Online (Sandbox Code Playgroud)
shop.selectors.js
import { createSelector } from "reselect"
const selectShop = state => state.shop
export const selectShopCollections …Run Code Online (Sandbox Code Playgroud) reactjs reselect react-router-dom react-hooks mapstatetoprops