我想回答新的javascript,react,react-native和node问题。那么,我如何知道用户在这些领域提出的新问题?
我使用 @testing-library/react-native 来测试我的 RN 应用程序。当我运行 yarn test 时,出现以下错误。
@testing-library/react-native should have "jest-preset.js" or "jest-preset.json" file at the root.
Run Code Online (Sandbox Code Playgroud)
我为我的应用程序使用打字稿。
我的测试脚本是这样的。
test": "jest --config jest.config.json"
Run Code Online (Sandbox Code Playgroud)
jest.config.json 文件是这样的。
{
"preset": "@testing-library/react-native",
"collectCoverageFrom": ["src/**/*.{ts,tsx}"],
"moduleDirectories": ["node_modules", "src"],
"setupFiles": [
"<rootDir>/jestSetup/setup.js",
"./node_modules/react-native-gesture-handler/jestSetup.js"
],
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?(react-native|@?react-navigation|@react-native-community))"
],
"coveragePathIgnorePatterns": ["/node_modules/", "/jestSetup", "/src/config.ts", "/src/app.tsx"]
}
Run Code Online (Sandbox Code Playgroud)
为什么我收到这个错误?
我想将 react-native-datepicker 文本左对齐。默认情况下它是居中的。我尝试更改 style 和 customstyle props,但没有用。
如何左对齐文本?
<DatePicker
style={styles.datePicker}
date={this.state.birthday}
mode="date"
placeholder="Birthday"
format="YYYY-MM-DD"
minDate="1950-01-01"
maxDate="2018-06-01"
confirmBtnText="Ok"
cancelBtnText="Cancel"
showIcon={false}
customStyles={{
dateInput: {
borderWidth: 0,
borderBottomWidth: 2
},
placeholderText: {
fontSize: 20,
color: "#C7C7C7"
},
dateText: {
fontSize: 20,
color: "black",
textAlign: "left"
}
}}
onDateChange={birthday => this.setState({birthday: birthday})}
/>
Run Code Online (Sandbox Code Playgroud) 我的 RN 应用程序中有以下代码。
<TouchableOpacity
// onPress={onPress}
onPressIn={this.handlePressIn}
onPressOut={this.handlePressOut}
disabled={disabled}
style={[
styles.button,
buttonStyle,
this.buttonStyles(),
]}
>
<Text>Hello</Text>
</TouchableOpacity>
Run Code Online (Sandbox Code Playgroud)
我想在按下 TouchableOpacity 时更改其颜色。因此,在 onPressIn 中,我将颜色更改为 darkRed,但它显示浅红色或类似的颜色。那里不能设置深色。这里有什么问题呢?如何更改颜色直到 onPressOut?颜色应该是深色的。
我正在学习 Udemy for Node js 课程。猫鼬预中间件与删除一起使用,如下所示。
ReviewSchema.pre('remove', function() {
// code goes here
});
Run Code Online (Sandbox Code Playgroud)
但我的实现不同,我想使用 findByIdAndDelete。这是我的代码。
ReviewSchema.pre('findByIdAndDelete', function() {
// code goes here
});
Run Code Online (Sandbox Code Playgroud)
但这个不会触发。我尝试在其中console.log,但它没有触发。我在这里做错了什么?
我想创建一个TextInput,当它有多行时可以自动增长。
<TextInput
placeholder="Type Comment"
value={this.state.comment.value}
onChangeText={value => this.onChangeComment(value)}
onPress={() => this.uploadComment()}
multiline={true}
maxLength={200}
numberOfLines={5}
/>
Run Code Online (Sandbox Code Playgroud)
我该如何实现?
我想在navigationOptions中访问我的组件状态.我试图将它分配给componentDidMount中的params.它仍然没有用.如何设置this.state.SearchText作为navigationOptions内输入的值?
export default class WorkoutScreen extends Component {
constructor(props) {
super(props);
this.state = {
searchText: "Test Text"
};
}
componentDidMount() {
this.props.navigation.setParams({
searchWorkouts: this.searchWorkoutHandler
});
}
onChangeSearch = value => {
this.setState({
searchText: value
});
};
searchWorkoutHandler = () => {
alert("Searching Workouts");
};
render() {
return (
<View>
<Text style={styles.searchInput}>{this.state.searchText}</Text>
</View>
);
}
static navigationOptions = ({ navigation }) => {
const { params = {} } = navigation.state;
alert(params.searchText);
return {
headerTitle: (
<SearchInput
style={styles.searchInput}
value={params.searchText}
source={Search}
borderRadius={50} …Run Code Online (Sandbox Code Playgroud) 我想创建一个像这样的反应组件。我希望这个 h1 标签是动态的。我尝试将其设置为道具,但没有成功。我怎样才能使这个h1标签动态化,这样,当我们使用这个组件时,我们可以将它设置为h1、h2、h3等。
import React from 'react';
import PropTypes from 'prop-types';
const Text = props => {
const { className, text } = props;
return (
<h1 className={className}>
{text}
</h1>
);
}
Text.propTypes = {
className: PropTypes.string,
text: PropTypes.string.isRequired
}
export default Text;
Run Code Online (Sandbox Code Playgroud) 我设置的新 react-native 项目现在使用打字稿。那么,既然 TS 已经解决了这个问题,那么是否需要使用 propTypes 进行类型验证?对我来说,我认为使用 propTypes 不会增加任何价值,因为 TS 处理类型转换问题。它是正确的还是应该使用 propTypes?如果有,有什么好处?
在我的 Node 应用程序中,我使用 @hapi.Joi 包进行验证。我有以下代码。
export function validateUser(user) {
const schema = Joi.object({
firstName: Joi.string().min(1).max(20).required(),
lastName: Joi.string().min(1).max(20).required(),
email: Joi.string().email().max(50).required(),
mobile: Joi.string().min(8).max(12).required(),
password: Joi.string().min(8).max(16).required(),
confirmPassword: Joi.ref('password'),
});
return schema.validate(user);
}
Run Code Online (Sandbox Code Playgroud)
但这不会检查确认密码是否是必需的。我尝试了 Joi.ref('password').required()。但这给了我一个错误。我该如何解决这个问题?
react-native ×7
reactjs ×4
node.js ×3
javascript ×1
jestjs ×1
joi ×1
mongodb ×1
mongoose ×1
react-native-testing-library ×1
typescript ×1