我目前正在学习React,我正在尝试弄清楚如何将它与Redux一起用于构建移动应用程序.我对两者如何相关/可用在一起感到困惑.例如,我在React https://www.raywenderlich.com/99473/introducing-react-native-building-apps-javascript中完成了本教程,但现在我想要为该应用程序添加一些reducers/actions以及我不确定那些与我已经做过的事情有什么关系.
我正在尝试使用图像背景和一些自定义样式创建自定义警报对话框,但似乎无法在react-native中使用Alert或AlertIOS来了解如何执行此操作.这可能,或者是否有一些图书馆可以帮助我?提前致谢.
在我的react-native应用程序中,我目前有一个User类,我在其中定义当前用户,如下所示:
class User {
static currentUser = null;
//other relevant code here
static getCurrentUser() {
return currentUser;
}
}
export default User;
Run Code Online (Sandbox Code Playgroud)
在另一个类中,我试图访问此currentUser的设置值.我无法弄清楚如何正确调用此函数; 我收到了错误User.getCurrentUser is not a function
.我应该以不同的方式调用此功能吗?
var User = require('./User');
getInitialState: function() {
var user = User.getCurrentUser();
return {
user: user
};
},
Run Code Online (Sandbox Code Playgroud) 我正在尝试在玩笑测试中模拟文件类型库。在我的 javascript 文件中,我按以下方式使用该库:
import * as fileType from 'file-type';
....
const uploadedFileType = fileType(intArray);
Run Code Online (Sandbox Code Playgroud)
然后,在我的玩笑测试中,我正在做:
jest.mock('file-type');
import * as fileType from 'file-type';
Run Code Online (Sandbox Code Playgroud)
然后尝试通过以下方式模拟响应:
fileType = jest.fn();
fileType.mockReturnValue({ ext: 'jpg' });
Run Code Online (Sandbox Code Playgroud)
但是,我收到错误"fileType" is read-only.
有人知道我做错了什么吗?提前致谢。
我目前有一个看起来像这样的Javascript对象:
Object {0: 8, 1: 9, 2: 10}
我试图获取对象中的单个项目的数量(即3),但无法弄清楚如何这样做.由于对象不是数组,我不能只调用.length()
.我试图huntsList[2].toString().split('.').length
用逗号分割项目并以这种方式计算它们但它返回1,因为它将整个对象转换为单个字符串,如下所示:["[object Object]"]
.
任何关于如何实现这一点的建议都表示赞赏.
我正在使用react-redux应用程序,由于某种原因,我调用的操作未到达reducer(我目前在其中只有一条log语句)。我已附上我认为相关的代码,我们将不胜感激。
在组件中的函数内调用的操作:
onSearchPressed() {
console.log('search pressed');
this.props.addToSaved();
}
Run Code Online (Sandbox Code Playgroud)
actions / index.js:
var actions = exports = module.exports
exports.ADD_SAVED = "ADD_SAVED";
exports.addToSaved = function addToSaved() {
console.log('got to ADD_SAVED step 2');
return {
type: actions.ADD_SAVED
}
}
Run Code Online (Sandbox Code Playgroud)
reducers / items.js:
const {
ADD_SAVED
} = require('../actions/index')
const initialState = {
savedList: []
}
module.exports = function items(state = initialState, action) {
let list
switch (action.type) {
case ADD_SAVED:
console.log('GOT to Step 3');
return state;
default:
console.log('got to default');
return state;
} …
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试为MongoDB中的每个Item文档添加价格.其中许多价格都有范围,即商品A的价格范围为50美元至100美元.如何在数据库中存储这些范围,然后根据这些值进行查询?我想为每个文档添加两个属性,lowPrice和highPrice,分别保持项目价格范围的最低值和项目价格范围的最高值.然后,当我查询时,我只会查询lowPrice小于或等于查询价格且highPrice大于或等于查询价格的文档.不过,我觉得可能有一种更有效的方法.任何建议表示赞赏!
我试图理解为什么我的笑话/酶测试失败了。我有一个组件,我使用compose
redux 中的函数,结构如下:
class MyComponent extends Component {
//code here
}
export default compose(
connect(mapStateToProps),
)(MyComponent)
Run Code Online (Sandbox Code Playgroud)
在我的笑话测试中,我这样做:
Import { MyComponent } from ‘app/MyComponent’;
describe(‘<MyComponent />’, () => {
let component;
beforeEach(() => {
props = {
id: ‘23423’
}
component = shallow(<MyComponent {…props} />);
}
it(‘Loads correctly’, () => {
expect(component.state(‘isLoading’)).toEqual(true);
expect(component.find(‘InnerComponent’).length).toBe(1);
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到诸如“无法读取未定义的属性‘状态’”之类的错误。我知道使用浅层渲染并不能提供我需要的确切结构,但我不确定还有什么方法可以尝试获得正确的结构。我尝试浅渲染包装的组件,然后在其中找到未包装的组件,就像这样,component = shallow(<MyComponent {...props} />).find('MyComponent').shallow();
没有运气。任何其他建议将不胜感激。`
我一直在尝试使用python中的datetime库来创建具有以下值的datetime对象:“ 0000-00-00 00:00:00”。但是,我不断收到错误消息,提示年份超出范围。如何正确初始化呢?
我想在 React 组件中执行以下操作:
<div>
{this.props.isOpen && this.state.isReady && <div> Hello! </div>}
</div>
Run Code Online (Sandbox Code Playgroud)
是否可以在 React 中使用多个布尔值来条件渲染组件?这可能会向用户呈现布尔值吗?