我正在参加D3.js的Udemy课程.
不幸的是,由于D3.js的新版本问世,因此没有关于其新语法的最新教程,因此我浏览了他们的API的wiki.
话虽这么说,我一直坚持如何翻译这段代码(版本3).我正在学习顺序量表
var data = [
{key: "Glazed", value: 132},
{key: "Jelly", value: 71},
{key: "Holes", value: 337},
{key: "Sprinkles", value: 93},
{key: "Crumb", value: 78},
{key: "Chocolate", value: 43},
{key: "Coconut", value: 20},
{key: "Cream", value: 16},
{key: "Cruller", value: 30},
{key: "Éclair", value: 8},
{key: "Fritter", value: 17},
{key: "Bearclaw", value: 21}
];
var w = 800;
var h = 450;
var margin = {
top: 20,
bottom: 20,
left: 20,
right: 20
};
var width …Run Code Online (Sandbox Code Playgroud) 很抱歉看起来是一个新手问题(一直工作到很晚,刚开始使用React)但是我想弄清楚如何只渲染一个n x n维的简单表.
例如,在我的组件中,渲染输出将是这样的:
<table id="simple-board">
<tbody>
<tr id="row0">
<td id="cell0-0"></td>
<td id="cell0-1"></td>
<td id="cell0-2"></td>
</tr>
<tr id="row1">
<td id="cell1-0"></td>
<td id="cell1-1"></td>
<td id="cell1-2"></td>
</tr>
<tr id="row2">
<td id="cell2-0"></td>
<td id="cell2-1"></td>
<td id="cell2-2"></td>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
其中每一行都有自己的id,以及每个单元格.
初始状态
constructor(props){
super(props);
this.state = {size: 3}
}
Run Code Online (Sandbox Code Playgroud)
是什么设置表的大小.
让我失望的是如何在JSX中实现for循环.
假设我有一个类组件,它有这样的东西:
export class Math extends React.Component {
...
someComponentMethod = numb => {
const sample = numb * 10
...
const result = numb -5
return result
}
Run Code Online (Sandbox Code Playgroud)
是否可以sample在 Jest 中对变量进行测试断言?
我一直在尝试实现一个给出两个数组的函数,
array1的元素用作过滤掉array2中元素的条件.
例如:
array1= [apple, grapes, oranges]
array2= [potato, pears, grapes, berries, apples, oranges]
Run Code Online (Sandbox Code Playgroud)
在加入函数之后,array2应该有这样的元素:
filter_twoArrays(array1,array2)
array2= [grapes, apples, oranges]
Run Code Online (Sandbox Code Playgroud)
我尝试了下面的代码,使用for循环和array.splice(),但我看到的问题是,当我使用splice方法时,似乎它改变了for循环中array2的长度:
function filter_twoArrays(filter,result){
for(i=0; i< filter.length; i++){
for(j=0; j< result.length; j++){
if(filter[i] !== result[j]){
result.splice(j,1)
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何优化过滤功能将非常感谢任何输入
干杯!
目前我正在研究FCC生命游戏,我能够弄清楚如何生成下一个应用程序状态(递增).
话虽这么说,我的下一步是弄清楚如何连续生成新的应用程序状态(生成).在这种情况下,我试图在我的reducer中实现setInterval和clearInterval .
我希望能够启动/暂停生成新一代(通过在state.start中切换状态)
我实现它的麻烦是范围问题.
这是我的减速机的一部分:
减速器/ reducers_grid.js
export default function(state = initialState, action){
switch (action.type) {
....
case START:
let toggleStart = state.start === true ? false : true;
if (state.start === true){
console.log('START THE GAME')
const newGeneration = nextGeneration(action.payload,state)
return Object.assign({}, state, {cells: newGeneration, start: toggleStart })
}
else {
console.log('PAUSE THE GAME')
return Object.assign({}, state, {start: toggleStart })
}
return state;
}
Run Code Online (Sandbox Code Playgroud)
从本质上讲,nextGeneration函数会生成新的应用程序状态(生成)并通过Object.assign将其更新
首先,我想将setInteveral放在第一个if语句中,将clearInterval放在第二个if语句中,但显然会产生范围问题.
这部分逻辑似乎非常微不足道,但我一直坚持这一点.
我尝试在互联网上查找,但找不到此错误的解决方案。
本质上,当我通过在本地主机中运行我的应用程序时npm start,它运行没有问题。
但是,当我切换到git branch gh-pagesgh-pages 中的主机时,我在 console.log 中看到以下错误
未捕获的不变违规:缩小的 React 错误 #37
_registerComponent(...): 目标容器不是 DOM 元素,这就是错误 #37 的含义。
这是我当前的设置:
webpack.config.js
var path = require('path');
var webpack = require('webpack');
var ghpages = require('gh-pages');
var WriteFilePlugin = require('write-file-webpack-plugin');
module.exports = {
devtool: 'source-map',
entry: [
'./src/index.js'
],
output: {
publicPath: '/',
filename: 'bundle.js',
sourceMapFilename: "./bundle.js.map",
path: path.join(__dirname, './static')
},
module: {
loaders: [{
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['react', …Run Code Online (Sandbox Code Playgroud) 所以我在实现这个场景时遇到了麻烦:
从后端服务器,我收到一个 html 字符串,如下所示:
<ul><li><strong>Title1</strong> <br/> <a class=\"title1" href="title1-link">Title 1</a></li><li><strong>Title2</strong> <a class="title2" href="/title2-link">Title 2</a>
Run Code Online (Sandbox Code Playgroud)
通常,仅使用dangerouslySetInnerHTML.
但是,围绕a hrefhtml 中的标签,我需要将它们包装在组件中,如下所示:
<ModalLink href={'title'}>
{Title}
</ModalLink>
Run Code Online (Sandbox Code Playgroud)
这是因为在包装a标签时,组件本质上添加了创建新子窗口的功能。
我想这是你需要在 Regex 上实现的东西,但我不知道从哪里开始。
因此,我在终端中收到此错误:
-bash(637,0x7fffa5467340) malloc: *** mach_vm_map(size=18446744071947448320) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
-bash: xrealloc: cannot allocate 18446744071947447824 bytes
Run Code Online (Sandbox Code Playgroud)
经过研究后,我相信这可能与我有关.bash_profile。
但是,我只知道如何通过终端打开隐藏文件,并且由于无法访问(由于此错误),如何通过Finder打开隐藏文件?
我正在React做一个小的食谱清单用于学习目的(因为我是新手).
我能够从列表中找出如何添加和删除配方.但是我很难从列表中编辑配方(App的状态).
以下是Redux的文档:
因为我们想要在不诉诸突变的情况下更新数组中的特定项,我们必须使用与索引处的项目相同的项创建一个新数组.
我很难在修改自己选择的数组时选择数组中的特定项.
这是我的动作文件:
SRC /行动
export const RECIPE_ADD = 'RECIPE_ADD';
export const RECIPE_EDIT = 'RECIPE_EDIT';
export const RECIPE_DELETE = 'RECIPE_DELETE';
export function addRecipe(recipe) {
return {
type: RECIPE_ADD,
payload: recipe
}
}
export function editRecipe(recipe) {
return {
type: RECIPE_EDIT,
payload: recipe
}
}
export function deleteRecipe(recipe) {
return {
type: RECIPE_DELETE,
payload: recipe
}
}
Run Code Online (Sandbox Code Playgroud)
SRC /减速器/ reducer_recipe
import { RECIPE_ADD } from '../actions/index';
import { RECIPE_DELETE } from '../actions/index';
import { RECIPE_EDIT } from '../actions/index' …Run Code Online (Sandbox Code Playgroud) 在请求文档中的一个示例中显示了此示例:
https://www.npmjs.com/package/request#custom-http-headers
var request = require('request');
var options = {
url: 'https://api.github.com/repos/request/request',
headers: {
'User-Agent': 'request'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info.stargazers_count + " Stars");
console.log(info.forks_count + " Forks");
}
}
request(options, callback)
Run Code Online (Sandbox Code Playgroud)
让我们说我希望变量信息的值返回给我.
我该怎么做?
我已经解决这个问题太久了,我似乎找不到我的逻辑有什么问题。
迅速的:
给定两个表示为字符串的非负整数 num1 和 num2,返回 num1 和 num2 的乘积。
笔记:
num1 和 num2 的长度都小于 110。
num1 和 num2 都只包含数字 0-9。
num1 和 num2 都不包含任何前导零。
您不得使用任何内置 BigInteger 库或直接将输入转换为整数。
这是我的尝试:
var multiply = function(num1, num2) {
var result = 0;
// if any of the nums is 0, automatically it is zero
if (num1[0] === '0' || num2[0] === '0') {
return '0'
};
var length1 = num1.length - 1;
var length2 = num2.length - 1;
var counterI = 0;
var …Run Code Online (Sandbox Code Playgroud)我有一个以下类组件:
export class HelloWorld extends Component {
constructor(props) {
super(props)
this.methodA = this.methodA.bind(this)
this.methodB = this.methodB.bind(this)
}
methodA(props) {
if (props.someValue === true) {
......
methodB(props.someValue, true)
} else {
......
methodB(props.someValue, false)
}
}
...
...
}
Run Code Online (Sandbox Code Playgroud)
本质上,我调用methodA某些methodB参数时必须传递它。
在开玩笑中,我很难在methodB被调用的地方编写测试覆盖范围methodB
describe('componentA', () => {
it('should call componentB', () => {
const wrapper = enzyme.shallow(<HellowWorld {...(Object.assign(baseProps, { smsStatus: 'VALID' }))} />)
const spyFn = {
methodB: () => jest.fn()
}
const spyPreventDefault = jest.spyOn(spyFn, …Run Code Online (Sandbox Code Playgroud) 我很难弄清楚如何增加数组中对象的值
例如,我有一个基于Poll模式的文档:
{
"_id": "584b2cc6817758118e9557d8",
"title": "Number of Skittles",
"description": "Test1",
"date": "Dec 9, 2016",
"__v": 0,
"labelOptions": [
{
"Bob": 112
},
{
"Billy": 32
},
{
"Joe": 45
}
]
}
Run Code Online (Sandbox Code Playgroud)
使用express,我能够做到这一点:
app.put('/polls/:id', function(req, res){
let id = req.params.id;
let labelOption = req.query.labelOption;
Poll.findOneAndUpdate(
{'_id' : id},
{$inc: {`labelOptions.$.${labelOption}`: 1 }},
function(err){
console.log(err)
})
Run Code Online (Sandbox Code Playgroud)
labelOption我想增加其价值的那个在哪里
更简洁地说,我在遍历文档内部时遇到问题。
javascript ×9
reactjs ×6
react-redux ×3
jasmine ×2
jestjs ×2
node.js ×2
reducers ×2
redux ×2
tdd ×2
algorithm ×1
array-splice ×1
arrays ×1
d3.js ×1
enzyme ×1
filter ×1
git ×1
loops ×1
macos ×1
mongodb ×1
mongoose ×1
path ×1
react-dom ×1
regex ×1
request ×1
setinterval ×1
terminal ×1
webpack ×1