我只是制作一个简单的应用程序来学习与redux的异步.我已经完成了所有工作,现在我只想在网页上显示实际状态.现在,我如何在render方法中实际访问商店的状态?
这是我的代码(一切都在一页,因为我只是在学习):
const initialState = {
fetching: false,
fetched: false,
items: [],
error: null
}
const reducer = (state=initialState, action) => {
switch (action.type) {
case "REQUEST_PENDING": {
return {...state, fetching: true};
}
case "REQUEST_FULFILLED": {
return {
...state,
fetching: false,
fetched: true,
items: action.payload
}
}
case "REQUEST_REJECTED": {
return {...state, fetching: false, error: action.payload}
}
default:
return state;
}
};
const middleware = applyMiddleware(promise(), thunk, logger());
const store = createStore(reducer, middleware);
store.dispatch({
type: "REQUEST",
payload: fetch('http://localhost:8000/list').then((res)=>res.json())
});
store.dispatch({ …
Run Code Online (Sandbox Code Playgroud) 我收到此错误,我在修复此问题时遇到了很多麻烦.
我在这里尝试做的是有3个不同的屏幕,并有一个导航到每个屏幕的tabbar.
这是我的索引:
import React, { Component } from 'react';
import { AppRegistry, Navigator, StyleSheet, View, Text } from 'react-native';
import Nav from './app/components/Nav';
import Screen from './app/Screen';
import Tabs from 'react-native-tabs'
import SwitchView from './SwitchView';
class Proj extends Component {
constructor(props){
super(props);
}
render(){
var x = "FeedView";
return(
<View style={styles.container}>
<Tabs selected={x} style={{backgroundColor:'white'}}
selectedStyle={{color:'red'}} onSelect={el=> {x = el.props.name}}>
<Text name="FeedView">First</Text>
<Text name="WikiView" selectedIconStyle={{borderTopWidth:2,borderTopColor:'red'}}>Second</Text>
<Text name="BoardView">Third</Text>
</Tabs>
<SwitchView id={x}/>
</View>
);
}
}
const styles = StyleSheet.create({
container: { …
Run Code Online (Sandbox Code Playgroud) 如何通过JSON文件解析检索其所有数据并在我的代码中使用它?
我已经尝试导入该文件并尝试使用控制台记录它,但它只是打印对象{}:
import jsonData from "./file.json";
console.log(jsonData);
Run Code Online (Sandbox Code Playgroud)
这是我的file.json看起来像:
[
{
"id": 1,
"gender": "Female",
"first_name": "Helen",
"last_name": "Nguyen",
"email": "hnguyen0@bloomberg.com",
"ip_address": "227.211.25.18"
}, {
"id": 2,
"gender": "Male",
"first_name": "Carlos",
"last_name": "Fowler",
"email": "cfowler1@gnu.org",
"ip_address": "214.248.201.11"
}
]
Run Code Online (Sandbox Code Playgroud)
我希望能够访问每个组件的名字和姓氏,并在网站上打印.
我试图跟踪每篇文章中的分数并显示它.但是,当它缺少对象中的向上或向下项目时,我会遇到问题.
这是我所有投票的数据:
const votes = {
"1": {
"up": [
"12345",
"12346"
],
"down": [
"22456"
],
},
"2": {
"up": [
"12345",
"12346"
],
"down": [
"22456",
"25214",
"23612"
]
}
}
export default votes;
Run Code Online (Sandbox Code Playgroud)
这是我如何处理它的权利,但这种要求必须有两个"up"
和"down"
的对象.
<div style={vote}>
{votes[id] ? votes[id].up.length - votes[id].down.length : 0 }
</div>
Run Code Online (Sandbox Code Playgroud)
因此,在此情况下,你必须只在up
元素,我希望得到的结果是唯一的了长度.同样的情况下来:
"3": {
"up": [
"12345",
"22341"
]
} //this would output a score of 2
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,谢谢!
我想在反应中修改一个 JSON 文件。有什么办法吗?
我有这个代码,当我点击一个 html 元素时它会运行。最后它调用 VoteTracking,这是我实际修改我的 json 文件的函数。
handleChange(val){
const { voted, score, imgup, imgdown, deviceKey } = this.state;
const { id } = this.props;
var x = this.state.score, vote = val;
var clr;
if (val) clr="#d98832"; else clr = "#3d3dff";
if(!voted) {
x += val;
this.setState({voted: val, score: x, color: clr });
}
else if(Math.abs(voted)){
if (voted == val){
vote = 0;
x -= val;
this.setState({voted: 0, score: x, color: "#6e6e6e"});
}
else {
x += (2* val); …
Run Code Online (Sandbox Code Playgroud) javascript ×5
reactjs ×5
json ×3
android ×1
asynchronous ×1
node.js ×1
react-native ×1
react-redux ×1
redux ×1