我正在分解Redux的todo示例以试图理解它.我读过,mapDispatchToProps
允许你将调度动作映射为道具,所以我想重写addTodo.js
使用mapDispatchToProps而不是调用dispatch(addTodo()).我打电话给它addingTodo()
.像这样的东西:
import React from 'react';
import {connect} from 'react-redux';
import addTodo from '../actions';
let AddTodo = ({addingTodo}) => {
let input;
return (
<div>
<form onSubmit={e => {
e.preventDefault()
if (!input.value.trim()) {
return
}
addingTodo(input.value)
input.value = ""
}}>
<input ref={node => {
input = node
}} />
<button type="submit">Submit</button>
</form>
</div>
)
}
const mapDispatchToProps = {
addingTodo: addTodo
}
AddTodo = connect(
mapDispatchToProps
)(AddTodo)
export default AddTodo
Run Code Online (Sandbox Code Playgroud)
但是,当我运行应用程序时,我收到此错误:Error: Invalid value of …
使用ReactJS,我有两个不同的API点,我试图得到和重组:students
和scores
.它们都是一个对象数组.
我的目标是:首先,获得学生和分数,其次,学生和分数保存在州,我将修改它们并根据学生和分数状态创建一个新状态.总之,我有3个功能:getStudents
,getScores
,和rearrangeStudentsAndScores
.getStudents
并且getScores
需要在完成之前完成rearrangeStudentsAndScores
.
我的问题是:有时rearrangeStudentsAndScores
会在getScores
完成之前运行.那搞砸rearrangeStudentsAndScores
了.但有时它会完成.不确定为什么它在50%的时间都有效,但我需要让它在100%的时间内都能正常工作.
这是我fetch
students and scores
在我的Client
文件中所要做的:
function getStudents(cb){
return fetch(`api/students`, {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}).then((response) => response.json())
.then(cb)
};
function getScores(cb){
return fetch(`api/scores`, {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}).then((response) => response.json())
.then(cb)
};
Run Code Online (Sandbox Code Playgroud)
然后我把它们组合在一起:
function getStudentsAndScores(cbStudent, cbScores, cbStudentsScores){
getStudents(cbStudent).then(getScores(cbScores)).then(cbStudentsScores);
}
Run Code Online (Sandbox Code Playgroud)
在我的反应应用程序中,我有以下内容:
getStudentsAndScores(){
Client.getStudentsAndScores(
(students) …
Run Code Online (Sandbox Code Playgroud) 我正在做一些编码挑战,遇到了一些我不太熟悉的东西.我更好奇地了解它是什么以及它为什么存在.
提示非常简单: Given a 32-bit signed integer, reverse digits of an integer.
Example:
Input: -123
Output: -321
Example:
Input: 120
Output: 21
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Run Code Online (Sandbox Code Playgroud)
我想出了这个.
var reverse = function(x) {
var isNegative = false;
if(x < 0){
isNegative = true;
x *= -1;
};
var reverseX = …
Run Code Online (Sandbox Code Playgroud) 我正在阅读react-redux上的API并查看Redux的一个github示例:Redux todo app
其中一个容器FilterLink
有mapDispatchToProps
(和mapStateToProps
)接受两个参数,其中一个是selfprops.
const mapDispatchToProps = (dispatch, ownProps) => ({
onClick: () => {
dispatch(setVisibilityFilter(ownProps.filter))
}
})
Run Code Online (Sandbox Code Playgroud)
API文档说:
"如果你的mapStateToProps函数被声明为带两个参数,它将以store状态作为第一个参数调用,props将作为第二个参数传递给连接组件,并且每当连接组件接收到新参数时也会重新调用它由浅等式比较确定的道具.(第二个参数通常按惯例称为ownProps.)"
我仍然无法完全掌握它的作用.有人可以解释一下ownProps
不同的例子吗?
比方说我有一个字符串string= "aasmflathesorcerersnstonedksaottersapldrrysaahf"
.如果你没有注意到,你可以"harry potter and the sorcerers stone"
在那里找到短语(减去空格).
我需要检查是否string
包含字符串的所有元素.
string.include? ("sorcerer") #=> true
string.include? ("harrypotterandtheasorcerersstone") #=> false, even though it contains all the letters to spell harrypotterandthesorcerersstone
Run Code Online (Sandbox Code Playgroud)
包含不适用于混洗字符串.
如何检查字符串是否包含另一个字符串的所有元素?
我在React中有一个基本表单,要求输入用户名.输入所需的用户名(比如说iggy
)之后,我想把它改为console.log用户名,iggy
.
事情就是这样:传统上,我会做类似的事情
constructor(){
super();
this.state={username: ''}
...
handleUsernameEnter: function(e){
this.setState({
username: e.target.value
})
},
...
<form onSubmit={this.handleUsernameSubmission}>
<input placeholder="enter username" ref="usernameItem" onChange={this.handleUsernameEnter} />
<input type="submit" value="Submit username" />
</form>
...
Run Code Online (Sandbox Code Playgroud)
我会在state
用户输入时存储用户名.没问题.这次,我还不想在状态中保存用户名.我希望用户在输入文本上输入用户名,当用户单击提交按钮时,handleUsernameSubmission
会以某种方式获取value
用户输入的用户名,以及console.log该值.我无法弄清楚如何将值传递input
给username
变量handleUsernameSubmission
.
handleUsernameSubmission: function(username){
console.log('username entered: ', username)
},
Run Code Online (Sandbox Code Playgroud)
JSFiddle:https://jsfiddle.net/iggyfiddle/adj4Ln1p/3/
如何将用户名从表单的输入值传递到username
变量handleUserSubmission
而不将其保存为状态?
我的直觉是我需要ref
在输入中使用,但我不确定如何引用onsubmit来获取value
特定输入.就像是<form onSubmit={this.handleUsernameSubmission(input.'usernameItem')}>
我已经在这个问题上停留了几天,但我不知道这是怎么回事。几个月前,我开始使用Ruby on Rails,目前正在学习API的身份验证。我已经在其他类似的主题看上去这里和那里,但没有他们的帮助。
我的问题是,无论何时我在此代码上运行RSpec(位于spec/api/v1/controllers/posts_controller_spec.rb
)
require 'rails_helper'
RSpec.describe Api::V1::PostsController, type: :controller do
let(:my_user) { create(:user) }
let(:my_topic) { create(:topic) }
let(:my_post) { create(:post, topic: my_topic, user: my_user) }
context "unauthenticated user" do
it "PUT update returns http unauthenticated" do
put :update, topic_id: my_topic.id, id: my_post.id, post: {title: my_post.title, body: my_post.body}
expect(response).to have_http_status(401)
end
...
Run Code Online (Sandbox Code Playgroud)
我不断
...
spec/api/v1/controllers/posts_controller_spec.rb -e "unauthenticated user"
Run options: include {:full_description=>/unauthenticated\ user/}
FFF
Failures:
1) PostsController unauthenticated user PUT update returns http …
Run Code Online (Sandbox Code Playgroud) 非常基本的问题,但不知何故我无法让它发挥作用。我试图让位于项目本地文件夹中的图像显示在 Rails 上。另外,我正在使用引导程序;class: "img-responsive"
因此我也需要声明。这是原始代码:<img class="img-responsive" src="assets/img/mockup/img2.jpg" alt="">
我查阅了这篇建议的帖子<%= image_tag("xyz.png", class: "img-responsive img-thumbnail img-circle") %>
和建议的rubyonrails 指南image_tag("/icons/icon.gif", class: "menu_icon")
。
我努力了
<%= image_tag ("assets/img/mockup/img2.jpg", class: "img-responsive")%>
Run Code Online (Sandbox Code Playgroud)
和
<%= image_tag "assets/img/mockup/img2.jpg" %>
Run Code Online (Sandbox Code Playgroud)
但它仍然不起作用。我可以确认路径是:app/assets/img/mockup/img2.jpg
如何在 Rails 应用程序视图上显示上述图像?
我正在学习JS全局变量和局部变量,但我对这个特定函数感到困惑.
var text = "top";
function print() {
return (text);
}
print();
//returns 'top'
Run Code Online (Sandbox Code Playgroud)
我明白为什么它会返回顶部.var text
是一个全局变量.print()
函数可以访问它并返回text
,从而返回'top'
.
var text = "top";
function print() {
return (text);
var text = "bottom";
}
print();
// returns undefined
Run Code Online (Sandbox Code Playgroud)
我有全局和局部变量的基本知识(或者我认为).我知道该函数print
可以访问自己的本地加全局变量.
我不明白为什么会这样undefined
.根据我的理解,该行return text;
检索text
它可以访问的全局变量(如第一个代码块所示).返回后text = 'top'
,它还声明了自己的局部变量,其名称相同但值不同'bottom'
.据bottom
我所知,局部变量应该坐在那里,因为之前没有调用它.
为什么它没有显示top
(甚至是节目bottom
)而是显示undefined
?
我看到一个代码片段使用make_ref()
了这个功能,但不确定它的可用性。
hexdocs 说:
返回一个几乎唯一的引用。
返回的引用会在大约 2^82 次调用后重新出现;因此它对于实际用途来说是足够独特的。
由编译器内联。
但它并没有真正说明何时或为什么我应该使用它。我为什么要使用它以及何时应该使用它?在我看来,它所做的只是生成随机数。为什么我不能只使用某种随机数生成器?
这是它在终端上的作用:
iex(1)> make_ref()
#Reference<0.3569050097.3772514305.191818>
iex(2)> make_ref()
#Reference<0.3569050097.3772514305.191837>
iex(3)> make_ref()
#Reference<0.3569050097.3772514307.194286>
Run Code Online (Sandbox Code Playgroud) javascript ×4
reactjs ×4
react-redux ×2
redux ×2
asynchronous ×1
elixir ×1
fetch ×1
forms ×1
image ×1
integer ×1
rails-api ×1
rspec ×1
ruby ×1
string ×1
variables ×1