首页 > 解决方案 > 使用带有 redux 和 react-redux 的 typescript 编写 react native

问题描述

我想知道当我在 React Native 中使用 typescript 编写 redux action、reducers 和 store 时,我应该给它一个扩展.tsx还是.js只能?

例如,这是我的store.tsx文件如下所示:

import {createStore, applyMiddleware, combineReducers} from 'redux';
import {composeWithDevTools} from 'redux-devtools-extension';
import thunk from 'redux-thunk';

import newsReducer from './reducers/newsReducer';

const rootReducer = combineReducers({
  news: newsReducer,
});

const middleware = composeWithDevTools(applyMiddleware(thunk));

export default createStore(rootReducer, middleware);

在这种情况下,我的文件是否应该成为所有tsx扩展名?

标签: reactjstypescriptreact-nativeredux

解决方案


如果您没有在该组件中使用任何 Typescript 代码,则不需要将其设为 .tsx,但无论何时使用任何 Typescript 功能,您都应该这样做,因为 typescript 编译器接受 .ts 文件并转译为 JS,

如果您在项目中的任何地方都使用过 .tsx ,那么我建议您仅使用 .tsx 来正确管理代码结构。

所以你可以相应地使用


推荐阅读