Reactjs 子组件中的导入操作和分派

Reactjs 子组件中的导入操作和分派,reactjs,react-native,react-redux,react-hooks,Reactjs,React Native,React Redux,React Hooks,如何导入我的操作而不是在bookmarkVideo中硬编码,以便在我的子组件中分派它 容器: import {bookmarkVideo} from '../actions/videos'; export default function VideoPlayerScreen(props) { const dispatch = useDispatch(); ... // import action above instead cons

如何导入我的操作而不是在
bookmarkVideo
中硬编码,以便在我的子组件中分派它

容器:

    import {bookmarkVideo} from '../actions/videos';
    export default function VideoPlayerScreen(props) {

      const dispatch = useDispatch();
      ...
    // import action above instead
       const bookmarkVideo = id => {
        dispatch({
           type: 'BOOKMARK_VIDEO',
           payload: id
        });
        navigate('Video Player');
  };

  return (
    <>
          <VideoPlayerHeader
            {...videoProps}
            onClick={bookmarkVideo}
          />
            ...
            </View>
const bookmarkVideoDispatcher = (id) => {
          const action = bookmarkVideo(id);
          dispatch(action);
}
减速器:

case "BOOKMARK_VIDEO":
  const newVideos = [];
  state.videos.map(item => {
    const { id, bookMarked } = item;
    const newBookmark = id == action.video ? !bookMarked : bookMarked;
    const newItem = {
      ...item,
      bookMarked: newBookmark
    };
    newVideos.push(newItem);
  });
  return { videos: newVideos, search: { term: "", videos: [] } };
商店:

import {createStore, applyMiddleware} from 'redux';
import {persistStore, persistReducer, autoRehydrate} from 'redux-persist';
import AsyncStorage from '@react-native-community/async-storage';
import thunk from 'redux-thunk';
import app from "../app.json";
import logger from 'redux-logger';
import rootReducer from '../reducers';

const persistConfig = {
    key: 'root',
    storage: AsyncStorage, // see "Merge Process" section for details.
    whitelist: [app.name],
    timeout: null,
};

const pReducer = persistReducer(persistConfig, rootReducer);

export const store = createStore(pReducer, applyMiddleware(thunk, logger));
export const persistor = persistStore(store);

export default store;

我认为您可以在容器中使用此功能:

    import {bookmarkVideo} from '../actions/videos';
    export default function VideoPlayerScreen(props) {

      const dispatch = useDispatch();
      ...
    // import action above instead
       const bookmarkVideo = id => {
        dispatch({
           type: 'BOOKMARK_VIDEO',
           payload: id
        });
        navigate('Video Player');
  };

  return (
    <>
          <VideoPlayerHeader
            {...videoProps}
            onClick={bookmarkVideo}
          />
            ...
            </View>
const bookmarkVideoDispatcher = (id) => {
          const action = bookmarkVideo(id);
          dispatch(action);
}
然后将其传递给onClick,如下所示:

<VideoPlayerHeader
        {...videoProps}
        onClick={bookmarkVideoDispatcher}
/>

我认为您可以在容器中使用此功能:

    import {bookmarkVideo} from '../actions/videos';
    export default function VideoPlayerScreen(props) {

      const dispatch = useDispatch();
      ...
    // import action above instead
       const bookmarkVideo = id => {
        dispatch({
           type: 'BOOKMARK_VIDEO',
           payload: id
        });
        navigate('Video Player');
  };

  return (
    <>
          <VideoPlayerHeader
            {...videoProps}
            onClick={bookmarkVideo}
          />
            ...
            </View>
const bookmarkVideoDispatcher = (id) => {
          const action = bookmarkVideo(id);
          dispatch(action);
}
然后将其传递给onClick,如下所示:

<VideoPlayerHeader
        {...videoProps}
        onClick={bookmarkVideoDispatcher}
/>

您需要调度两次,一次从UI到中间件,一次从中间件到reducer-


您需要调度两次,一次从UI到中间件,一次从中间件到reducer-


您是否在使用
thunk
?是的,请参阅更新的问题您是否在使用
thunk
?是的,请参阅更新的问题