我正在学习 Redux,我对这里发生的事情感到非常困惑。我正在使用 thunk 并且 GET_ITEMS 在我的减速器中,所以我不确定我做错了什么?错误在dispatch(getItemsAction());
Redux.js
function reducer(state, action) {
switch (action.type) {
case 'GET_ITEMS':
return {
...state,
items: action.payload,
loading: false,
};
case 'ADD_ITEM':
return {
...state,
items: [...state.items, action.payload],
};
case 'DELETE_ITEM':
return {
...state,
items: state.items.filter(item => item.id !== action.payload),
};
case 'ITEMS_LOADING':
return {
...this.state,
loading: true,
};
default:
return state;
}
}
export const getItemsAction = () => ({
return(dispatch) {
axios.get('api/items').then(response => {
console.log(response);
dispatch({ type: 'GET_ITEMS', payload: response.data });
});
}, …Run Code Online (Sandbox Code Playgroud) 我想调度一个取决于功能组件中先前设置的状态的操作。我有什么选择?
<Buton
onPress={() => {
setState(state + 1);
dispatch(myAction(state));
}}
/>
Run Code Online (Sandbox Code Playgroud)
编辑:为了清楚起见,我的意思是调度状态取决于之前设置的状态。在伪代码中:
async () => {
await setState(state + 1);
dispatch(myAction(state));
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试通过 ButtonRender 实现 android 按钮的功能。当我尝试加入“.LongClick”和“.touch”时会出现问题,因为它不会启动长按事件。
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
this.SetBackgroundResource(Resource.Drawable.button);
base.OnElementChanged(e);
Android.Widget.Button thisButton = Control as Android.Widget.Button;
thisButton.LongClickable = true;
thisButton.LongClick += delegate
{
string s = "";
};
thisButton.Touch += (object sender, Android.Views.View.TouchEventArgs e2) =>
{
if (e2.Event.Action == MotionEventActions.Down)
{
.
.
.
}
else if (e2.Event.Action == MotionEventActions.Up)
{
.
.
.
}
else if (e2.Event.Action == MotionEventActions.HoverExit || e2.Event.Action == MotionEventActions.Cancel)
{
.
.
.
}
else if (e2.Event.Action == MotionEventActions.Move)
{
.
. …Run Code Online (Sandbox Code Playgroud) 调用fetch函数时出错.
Undefined is not a function Error in Backbone js
Run Code Online (Sandbox Code Playgroud)
var models = Backbone.Model.extend({
userId: function() {
return this.get('userId');
},
id: function() {
return this.get('id');
},
body: function() {
return this.get('body');
}
});
//Collections
var todolistStore = Backbone.Collection.extend({
url: function() {
return 'https://jsonplaceholder.typicode.com/posts'
},
model: models,
parse: function(response) {
return response;
}
});
todolistStore.fetch();Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js" type="text/javascript"></script>Run Code Online (Sandbox Code Playgroud)
我试图想出循环遍历值数组的方法,将每个值保存为键并为每个键分配一个“true”布尔值。
目标是创建以下对象:
{
"arrayValue" : true,
"anotherArrayValue" : true,
"arrayValueAsWell" : true
}
Run Code Online (Sandbox Code Playgroud)
我在做:
var objectToCreate = {};
let myArray = ["arrayValue", "anotherArrayValue", "arrayValueAsWell"]
let myArrayConverted = myArray.forEach((prop,index) => objectToCreate[prop] = true)
Run Code Online (Sandbox Code Playgroud)
并得到:
{
0 : "arrayValue",
1 : "anotherArrayValue",
2 : "arrayValueAsWell"
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将两个对象合并为一个,但是我需要确保对象值和键的第一个数组成为另一个对象数组的键值,希望这是有道理的:
所以我有一个像这样的一些信息的dataConstant:
export const tileData = [
{
title: 'JS CONS',
skill: 40,
visitTitle: 'Visit JS',
visitUrl: 'https://www.google.co.uk'
},
{
title: 'JS CONS',
skill: 50,
visitTitle: 'Visit JS',
visitUrl: 'https://www.google.co.uk'
}
]
Run Code Online (Sandbox Code Playgroud)
我将此常量导入到我的React组件中。在同一个内部,我将导入多个图像,并将它们放入对象数组中,如下所示:
//the values of the objects keys are imported logos
getLogos(tileData) {
const logo = [{logo: js}, {logo: react}];
var combi = [...logo, ...i];
console.log(combi);
return combi;
}
Run Code Online (Sandbox Code Playgroud)
结果是:
0: {logo: "/static/media/js.a672e76f.png"}
1: {logo: "/static/media/react.5513eea1.png"}
2: {title: "JS CONS", skill: 40, visitTitle: "Visit JS", visitUrl: "https://www.google.co.uk"}
3: {title: …Run Code Online (Sandbox Code Playgroud) 我可以使用默认端口 3000 启动 react js。但是,当我将端口自定义为 4200 时"start": "PORT=4200 react-scripts start"(只是在 package.json 中更改),我无法启动 react js 应用程序。
'PORT' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! todo-app@0.1.0 start: `PORT=4200 react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the todo-app@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional lo
ging output above. …Run Code Online (Sandbox Code Playgroud) 在我的create-react-app / redux中,我有一个“连接的组件”:
import React from "react";
import "./App.css";
import { connect } from "react-redux";
function App() {
debugger;
const { datas } = this.props;
debugger;
return <div className="App">{datas}}</div>;
}
const mapStateToProps = state => {
return {
datas: state
};
};
// const mapDispatchToProps = dispatch => {
// return {
// onRequestDog: () => dispatch({ type: "API_CALL_REQUEST_POST" })
// };
// };
export default connect(
mapStateToProps,
{}
)(App);
Run Code Online (Sandbox Code Playgroud)
Redux在index.js中配置:
import React from 'react';
import ReactDOM from 'react-dom';
import …Run Code Online (Sandbox Code Playgroud) 我关注的是Pure React,有一个House组件需要一些构建(为简洁起见,省略了导入):
class House extends React.Component {
state = {
bathroom: true,
bedroom: false,
kitchen: true,
livingRoom: false
}
flipSwitch = (action) => {
this.setState({
???????????????
});
}
render () {
return (
<>
<RoomButton room='kitchen' handler={this.flipSwitch}/>
<RoomButton room='bathroom' handler={this.flipSwitch}/>
<RoomButton room='livingRoom' handler={this.flipSwitch}/>
<RoomButton room='bedroom' handler={this.flipSwitch}/>
</>
);
}
}
const RoomButton = ({room, handler}) => (
<button onClick={handler}>
{`Flip light in ${room}!`}
</button>
)
ReactDOM.render (
<House/>,
document.getElementById('root')
)
Run Code Online (Sandbox Code Playgroud)
预期的结果:单击<room>按钮时,House组件的状态会发生变化,以反映房间内电灯开关的翻转(即,true点亮,false熄灭)。
我想知道应该 …
我正在尝试更改状态挂钩,但出现此错误...我知道另一种更改方法,但它应该适用于传播语法,对吗?
export default function App() {
const [state, setState] = {
sld_url: "",
geojson_url: "",
}
const handleSldUrlChange = event => {
setState({...state, sld_url: event.target.value})
}
return (
<TextField
label="SLD URL"
value={state.sld_url}
className={classes.textField}
onChange={handleSldUrlChange}
margin="normal"
variant="outlined"
/>
);
}
Run Code Online (Sandbox Code Playgroud) javascript ×9
reactjs ×6
react-hooks ×2
react-redux ×2
redux ×2
backbone.js ×1
node.js ×1
redux-thunk ×1
xamarin ×1