所以我一直在研究这个问题,并认为最好重构我的代码,以便将状态设置为一个对象数组.我想要做的是按一下按钮增加一个数字.
我在一个组件中有一个回调函数,它触发一个函数来更新状态......但是我很难在对象中定位键值.
我的初始状态如下:
getInitialState: function() {
return {
items: [
{
links: 'zest',
trackId: 1023,
songTitle: 'z know the others',
artist: 'zuvet',
upVotes: 0
},
{
links: 'alpha',
trackId: 987,
songTitle: 'ass',
artist: 'arme',
upVotes: 3
},
]
}
Run Code Online (Sandbox Code Playgroud)
我试图锁定upVotes密钥,但无法弄清楚如何.我的函数传递一个键,以便我可以在数组中定位索引,但是当我尝试执行以下操作时:this.setState({items[key]: {upVotes: this.state.items[key].upVotes + 1}})由于意外的[令牌,它会引发错误.
我在这里尝试了类似于这个线程的东西,但我一直在收到错误.
我可以编写什么样的函数来设置我想要定位的对象中的键的State?
我有一个 html 模板,我正在使用模板文字。该函数如下所示
// postCreator.js
export const blogPostMaker = ({ title, content, address, id }, deletePost) => {
const innerHtml = `
<blog-post>
<h1 class='title'>${title}</h1>
<p class='content'>${content}</p>
<p class='address'>${address}</p>
<button onclick='${() => deletePost(id)}'>Delete</button>
</blog-post>
`
return innerHtml
}
//Blog.js
postHelper.getSeriesOfPosts(10)
.then(resp => {
resp.forEach(post => (blogDiv.innerHTML += blogPostMaker(post, postHelper.deletePost)))
})
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚的是如何让 onclick 工作。我试过将 anon 函数传递Blog.js给 thepostCreator也没有运气。
有任何想法吗?
所以我正在使用soundcloud API来吸引用户的收藏.它们的最大限制是每个请求200个,但是在对象的最后,它们有一个a_href键,其值是下一页的收藏夹.
基本上,我正在尝试放置一个按钮,以便用户可以点击它,它将为他们提供接下来的200个喜欢.我的问题是无需多次调用原始API URL即可访问data.a_href.我的代码看起来像这样:
function getAPIURL(username, subSection){
apiurl = "https://api.soundcloud.com/users/" + username + "/" + subSection + "/?client_id=" + clientID + limit + "&linked_partitioning=1"
}
function getFavorites(){
titleList.length = 0;
artistList.length = 0;
imgList.length = 0;
idList.length = 0;
$(".trackList").html("");
username = $("input").val();
subSection = "favorites";
getAPIURL(username, subSection);
getAPI(apiurl);
}
function getAPI(apiurl){
$.getJSON(apiurl, function(data) {
//Does some stuff then
$(".nextPage").on("click", function(){
titleList.length = 0;
artistList.length = 0;
imgList.length = 0;
idList.length = 0;
$(".trackList").empty();
getAPI(data.next_href);
})
});
}
Run Code Online (Sandbox Code Playgroud)
当我进入第二页时,上面的工作很棒.然而,当我尝试去第三页时,就像它在调用第三页之前调用第二页和第一页一样....
有任何想法吗?
好的,所以我知道iOS不允许自动播放,但SoundCloud如何自动播放其移动网站上的下一首歌?
我可以获得我要填写iframe src的下一首歌曲,并重新加载小部件以显示该曲目.
我已经尝试了很多变通办法,即使我在下一首曲目准备就绪时调用'sc.play()'方法,我仍然无法自动播放.iframe上的橙色大按钮变为暂停按钮,但它不会播放.我必须再次按下按钮才能开始播放.
用户启动第一次播放很好,但SC如何自动播放下一首曲目?
我认为这个问题是由于对节点缺乏了解,但我正在研究创建一个带有覆盆子pi和节点的运动传感器.
我不明白如何让我的节点服务器运行.我可以按预期使用它,setInterval但我不认为这是我应该这样做的.
基本上我希望能够启动程序node index.js并让它继续观察传感器连接的GPIO引脚,看看是否发生了什么.如果发生了某些事情,那么它会做一些事情,但在发生更多情况时会继续观察传感
我为保持运行而采取的措施与此类似:
var foo = require('require necessary things up here');
setInterval(function(){
//code for detecting sensor stuff here
}, 1000)
Run Code Online (Sandbox Code Playgroud)
这有效,但我知道我不认为这是正确的方法.
如果我执行类似下面的操作,它只执行功能,记录到控制台,但不会注意更改,只是退出.
var foo = require('require necessary things up here')
function checkForSensorStuff(){
//code for detecting sensor stuff here
console.log('checking stuff')
}
Run Code Online (Sandbox Code Playgroud)
如何让服务器保持运行,以便在不使用setInterval的情况下持续监视函数的变化?
在React Native的ScrollView中是否可以有多个onScroll事件?
我遇到的问题是我有一个渲染ListView像这样的组件:
<ListView
onScroll={Animated.event(
[{nativeEvent: {contentOffset: {y: this.state.scrollY}}}],
{onScroll: this.props.onScroll}
)}
scrollEventThrottle={10}
dataSource={stuff}
removeClippedSubviews={false}
renderSeparator={this.makeSeparator}
renderRow={this.makeStuff}
enableEmptySections={true}
/>
Run Code Online (Sandbox Code Playgroud)
如您所见,已经订阅了一个Animaion事件,但是我也希望能够传递this.props.onScroll,以防我希望呈现此事件的更高组件具有不同的滚动功能。
这可能吗?有任何想法吗?
javascript ×6
ios ×2
soundcloud ×2
android ×1
cron ×1
firebase ×1
html ×1
jquery ×1
json ×1
node.js ×1
raspberry-pi ×1
react-native ×1
reactjs ×1