我在 react-redux 和 eslint 中使用 react 。在我的容器中,导入一个将绑定到mapDispatchToProps中组件的props的动作,如下所示:
// simplified for brevity
import { getGames } from '../actions/game';
const mapDispatchToProps = dispatch => ({
getGames: () => dispatch(getGames())
});
class App extend React.Component {
// class members
}
export default App connect(mapDispatchToProps)(App);
我在componentDidMount中调用getGames:
componentDidMount () {
const { getGames } = this.props;
getGames().then(json => console.log(json));
}
Eslint坚持我使用对象分解来从componentDidMount中的this.props中提取值,这很好。
问题是,eslint还抱怨当我使用对象分解时,已经在外部范围(import语句)中声明了getGames。
在这两种情况下,我都希望保留,但是想不出在导入和 this.props分解中如何使用对象分解而不会引起命名冲突。
任何想法都值得赞赏!
答案 0 :(得分:2)
// simplified for brevity
import { getGames } from '../actions/game';
您可以为其使用别名。
import { getGames as getGames1 } from '../actions/game';
并使用别名代替。