我有一个存储在我的应用程序状态的JSON数据数组.它包括详细页面需要的属性(大约10个不同的字段).
我的列表视图只需要其中两个.例如,:
[
{
"id": 1,
"title": 'Derp',
"subtitle: 'Derp',
"another": 'Derp,
//... more attributes
},
{
"id": 2,
// ... etc
}
]
Run Code Online (Sandbox Code Playgroud)
我的列表视图仅涉及id,title和subtitle.获取相同数据的最简洁方法是什么,但只能使用我指定的密钥?
我应该使用地图吗?还是解构?
编辑:非常感谢警方出现的努力答案.这对我帮助很大,我确实尝试了一段时间来弄清楚如何做到这一点.我来自Ruby,所以ES6不是我的专长.
用例是仅从包含所有数据属性的全局Redux存储返回React组件所需的内容.例如,我的列表视图只需要来自JSON数据的2/3属性.我正在使用redux 选择器来做到这一点.
第二个/帖子后问题:这种类型的方法有没有名称,你只接受部分JSON?我想在redux选择器中创建一个方法,我需要提出一个名称.我想把它称为减速器,但这并不是很有效,因为术语reducer已经在Redux世界中用于修改状态的方法.
首先,我正在使用React Native。我从Firebase获取数据,并想快速写入存储区(由Redux提供)。但这行不通。您可以在下面找到我的所有代码:
功能:
async getTumData (uid) {
const {selectedGroupDetail, getSelectedGroupDetail} = this.props;
var yeniGrupDetayi = {};
await firebase.database().ref("/groups/"+uid).once('value').then(
function(snapshot){
yeniGrupDetayi = {...snapshot.val(), uid: uid};
}).catch(e => console.log(e.message));
console.log("FONKSIYON ICERISINDEKI ITEM ==>", yeniGrupDetayi);
this.props.getSelectedGroupDetail(yeniGrupDetayi);
console.log("ACTION'DAN GELEN ITEM ===>", selectedGroupDetail);
}
Run Code Online (Sandbox Code Playgroud)
行动:
export const getSelectedGroupDetail = (yeniGrupDetayi) => {
return {
type: GET_SELECTED_GROUP_DETAIL,
payload: yeniGrupDetayi
}
};
Run Code Online (Sandbox Code Playgroud)
减速器:
case GET_SELECTED_GROUP_DETAIL:
return { ...state, selectedGroupDetail: action.payload}
Run Code Online (Sandbox Code Playgroud)
Ç?kt ?:
FONKSIYON ICERISINDEKI ITEM ==> {admin: {…}, groupDescription: "Yayg?n inanc?n tersine, Lorem Ipsum rastgele sözcü…erini inceledi?inde …Run Code Online (Sandbox Code Playgroud) 我想将参数传递到自定义 Redux 中间件中。但我不知道如何在自定义中间件中使用参数。
例子:
import { createStore, applyMiddleware } from 'redux';
import reducers from './reducers';
const customMiddleWare = store => next => action => {
// how to use argument here?
};
const middlewares = [customMiddleWare(argument)];
const store = createStore(
reducers,
applyMiddleware(...middlewares)
);
Run Code Online (Sandbox Code Playgroud) 我一直在创建一个Reactjs应用程序。我遇到了一个问题,想知道从Firebase哪里获取数据。在根组件(App.jsx)或单个组件中获取数据集合是否更好?
现在,我有用户,产品和销售集合。一些组件使用相同集合中的数据。例如:“付款”组件还使用“销售”收款数据进行分析。
情况1:如果我在根组件中获取数据,则不必在组件内部获取数据。
情况2:如果我在组件内部获取数据,则将针对不同路径中的不同组件获取两次相同的数据。
请给我建议更好的处理情况的方法。
您好,我正在尝试redux从给定的数据中获取存储的初始数据api!
这是我使用的第一种方法store.dispatch:
import { createStore, applyMiddleware, compose } from "redux";
import thunk from "redux-thunk";
import reducer from "./reducer";
import { fetchNews } from "./actions";
const store = createStore(
reducer,
compose(
applyMiddleware(thunk),
window.__REDUX_DEVTOOLS_EXTENSION__ &&
window.__REDUX_DEVTOOLS_EXTENSION__()
)
);
store.dispatch(fetchNews());
export default store;
Run Code Online (Sandbox Code Playgroud)
第二种方法是从组件生命周期方法分派操作,如下所示:
import React from "react";
import axios from "axios";
import InfiniteScroll from "react-infinite-scroller";
import { useSelector, useDispatch } from "react-redux";
import New from "./New";
import { fetchNews } from "../store/actions";
const Main = () …Run Code Online (Sandbox Code Playgroud) 我正在使用 React-redux,在这里我编写了一个演示: 演示 ,我的问题是我需要在收到状态更改后执行一些操作,例如刷新页面。但我不知道如何订阅商店的状态变化。
谁能帮我?
我开始学习react,对包的概念很困惑。为什么我们不能只使用一个简单的链接作为 cdn 并且有一个我不理解的模块以及什么是 npm 以及为什么我必须将它与 React 一起使用
如果用户是:
1. Is logged in
2. Is not administrator
3. Is not a moderator
Run Code Online (Sandbox Code Playgroud)
所以我的代码是这样的:
<h2>Write a Review</h2>
{errorResortReview && <Message variant='danger'>{errorResortReview}</Message>}
{userInfo && (userInfo.role !== 'administrator' || userInfo.role !== 'resortOwner') ? (
<form onSubmit={submitHandler}>
<div className="form-group">
<label for="rating">Rating</label>
<ReactStars
count={5}
onChange={setRatingInput}
size={34}
activeColor="#ffd700"
/>
</div>
<div class="form-group">
<textarea className="form-control" value={comment} onChange={(e) => setComment(e.target.value)} id="comment" rows="3"></textarea>
</div>
<button type="submit" className="btn btn-primary">Submit</button>
</form>
) : <Message>Please <Link to='/login'>Login</Link> to write a review</Message>}
</li>
Run Code Online (Sandbox Code Playgroud)
当我在我的反应组件上尝试这个时,即使用户是管理员和版主,它仍然显示表单。
注意:userInfo 保存着当前登录用户的数据。
任何人都可以在这里发现问题吗?
我开始学习 ngRx 并且遇到了基本问题。在我的课堂上,我有:
@select() todos;
Run Code Online (Sandbox Code Playgroud)
然后,当我按下按钮时,我想过滤该数据并返回 html 过滤数据。尝试过类似的东西
public onSubmit(): void {
const example = this.todos.filter(todo => todo.lang === 'pl');
console.log(example);
Run Code Online (Sandbox Code Playgroud)
}
但我收到错误:
错误 TypeError:this.todos.filter 不是函数
有人有什么想法吗?
我没有在附近完成,但当我尝试npm开始这个项目我得到错误:
Unexpected token, expected ;
2 |
3 |
> 4 | const App = fucntion() {
| ^
5 | return <div>HI!</div>;
6 | }
7 |
Run Code Online (Sandbox Code Playgroud)
这是index.js的完整文件:
import React from 'react';
const App = fucntion() {
return <div>HI!</div>;
}
//shove into dom (page)
React.render(App);
Run Code Online (Sandbox Code Playgroud) redux ×10
javascript ×7
reactjs ×7
react-redux ×4
firebase ×2
node.js ×2
angular ×1
database ×1
ecmascript-6 ×1
express ×1
ngrx ×1
npm ×1
react-native ×1
redux-thunk ×1