有没有办法将规则添加到 eslintrc 中,其中 src 文件夹中的每个 .js 或 .jsx 文件都必须包含 proptypes 和/或 defaultprops,如果没有,则会引发错误。
我写的一些代码有一个小问题,我不断收到错误:
期望一个赋值或函数调用,而是看到一个表达式
我已经评论了错误发生的地方。但是不确定是什么问题。代码如下:
renderBody() {
const { bookings } = this.props;
if (bookings.length > 0) {
return (
Object.keys(bookings).map(e => (
<TableBody>
{Object.values(bookings[e]).forEach((value) => {
<TableCell>{value}</TableCell>; //<-ERROR??
})}
</TableBody>
))
);
}
return null;
}
Run Code Online (Sandbox Code Playgroud) 目前在 ReactJS 和路由方面有一个小问题。如果令牌不存在,我目前希望我的应用程序重定向回登录屏幕。该应用程序应该对其他页面的访问为零。如果用户尝试输入 url 以在没有有效令牌的情况下对其进行重定向,则应默认返回登录页面。
目前我的代码如下。
import { BrowserRouter, Route, Redirect, Switch } from 'react-router-dom';
...
render() {
const token = localStorage.getItem('access_token');
return (
<BrowserRouter>
{!token ? <Route render={() => <Redirect push to="/" />} /> :
<Switch>
<Route exact path="/" component={Login} />
<Route exact path="/validate" component={ValidateLoginContainer} />
<Route exact path="/home" component={Landing} />
<Route exact path="/non-arrivals" component={NonArrivals} />
<Route exact path="/due-in-summary" component={DueInSummary} />
<Route exact path="/due-in-forecast" component={DueInForecastDetail} />
<Route exact path="/hourly-arrivals-departures" component={HourlyArrivalsDepartures} />
<Route exact path="/overstays" component={Overstays} />
</Switch>
}
</BrowserRouter> …Run Code Online (Sandbox Code Playgroud) 我在使用 React Native Elements 文本输入以及使用可触摸不透明度时遇到了一个非常特殊的问题。
import React, { useState } from 'react';
import { TouchableOpacity, View, Dimensions } from 'react-native';
import { Input } from 'react-native-elements';
const test = () => (
<TouchableOpacity onPress={() => console.log('we hit here')}>
<Input disabled>
{children}
</Input>
</TouchableOpacity>
)
export default test;
Run Code Online (Sandbox Code Playgroud)
因此输入字段的外缘是完全可点击的,但是组件的中心却无法点击。
不过,这对于 Android 来说非常有效。
有任何想法吗
不确定我的代码有什么问题,但是我无法获得一个简单的 div 来渲染它在对象键循环中。我知道它循环了 11 次,因为我已经将控制台日志放入其中并输出了 7 次 console.log,但是,div 根本不呈现。有任何想法吗:
class BookingView extends React.Component {
render () {
const {color, booking} = this.props
return (
<div>
<BookingHeader booking={booking} color={color}/>
{Object.keys(booking).forEach(function(key,index) {
<div>this is a test</div>
})}
</div>
)
}
}
Run Code Online (Sandbox Code Playgroud) 尝试合并二维数组中的值时遇到一些问题。
例如:
const formatBids =
[
[4444, 10000],
[4444, 500],
[4455, 500],
];
Run Code Online (Sandbox Code Playgroud)
如果第一个参数相同,我想对数组的第二个参数求和,所以最终结果是:
[
[4444, 10500],
[4455, 500],
];
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法,但它不断地添加自身,产生错误的结果:
formatBids.map(bid => {
const map = new Map([bid]);
formatBids.forEach(arr => (arr[1] += map.get(arr[0]) ?? 0));
});
Run Code Online (Sandbox Code Playgroud)
不确定解决这个问题的最佳方法。有想法吗?