小编Yil*_*maz的帖子

在Redux Reducer中对对象数组进行排序

我试图排序这样的数组(我的状态):

[
        {
            name:"Aenean in justo ante"
        },
        {
            name:"Phasellus viverra mattis dolor"
        }
]
Run Code Online (Sandbox Code Playgroud)

我向减速器发送了一个动作:(减速器的一部分)

case 'SORT_COLLECTION':
        return state.sort(function(a, b){
    var nameA=a.name.toLowerCase(), nameB=b.name.toLowerCase()
    if (nameA < nameB) 
        return -1 
    if (nameA > nameB)
        return 1
        return 0 
})
Run Code Online (Sandbox Code Playgroud)

但这不起作用有人能告诉我错误在哪里吗?

javascript sorting jsx reactjs redux

4
推荐指数
1
解决办法
5252
查看次数

Solidity:ParserError:预期的编译指示、导入指令或合约/接口/库定义

当我编写简单合约时,我也遇到了最新的 solc(0.5.2 版本)和 0.4.25 的错误

我尝试过以下步骤

  1. 已卸载的 Solc: npm uninstall solc
  2. 安装的目标版本: npm install --save solc@0.4.25
  3. 节点compile.js(代码如下)

      { contracts: {},
      errors:
       [ ':1:1: ParserError: Expected pragma, import directive or contract
     /interface/library definition.\nD:\\RND\\BlockChain\\contracts\\Inbox.sol\n^\n' ],sourceList: [ '' ],sources: {} }
    
    Run Code Online (Sandbox Code Playgroud)

编译.js

const path  = require('path');
const fs = require('fs');
const solc = require('solc');
const inPath = path.resolve(__dirname,'contracts','Inbox.sol');
const src =  fs.readFileSync(inPath,'UTF-8');
const res = solc.compile(inPath, 1);

console.log(res);
Run Code Online (Sandbox Code Playgroud)

收件箱.sol

pragma solidity ^0.4.25;

contract Inbox {
    string  message;


    function Inbox(string passedName) public {
        message …
Run Code Online (Sandbox Code Playgroud)

node.js blockchain ethereum solidity smartcontracts

4
推荐指数
1
解决办法
2万
查看次数

Formik 的 innerRef 为 null

当我尝试获取 formik 上的引用时,我在 ref 对象的当前字段中得到 null 。

const formikRef = useRef();
...

  <Formik
    innerRef={formikRef}
    initialValues={{
      title: '',
    }}
    onSubmit={(values) => console.log('submit')}
  >
Run Code Online (Sandbox Code Playgroud)

接下来:

useEffect(() => {

    console.log(formikRef);
  }, []);
Run Code Online (Sandbox Code Playgroud)

得到:

Object {
  "current": Object {
    "current": null,
  },
}
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?请帮助。

javascript reactjs react-native

4
推荐指数
1
解决办法
1万
查看次数

即使数据没有改变,react redux useSelector 也会重新渲染

我正在使用 redux 和 redux thunk 进行反应。我有一个动作,我正在发出网络请求。使用 useSelector 我从 redux 存储中获取数据。我有一个问题,每次分派动作时组件都会重新渲染。我希望该组件仅在数据更改时重新渲染。我已经尝试使用shallowEqual 作为useSelector 的第二个参数。但它不起作用。我已将其压缩到此沙箱中的最小示例。在控制台中,您可以看到组件随着每个网络请求重新呈现。这是代码和框:https ://codesandbox.io/s/useselector-js6j0 ? file =/ src/App.js:885-1294

这是代码:

function LevelThree() {
  console.log(`Level three calls: ${++levelThreeCalls}`);
  const contextData = useSelector(state => state.data);
  console.log(contextData);
  const dispatch = useDispatch();
  useInterval(() => {
    dispatch(updateData());
  }, 1000);

  return (
    <div>
      <button onClick={() => dispatch(updateData())}>Change context</button>
      {contextData[0].userId}
    </div>
  );
Run Code Online (Sandbox Code Playgroud)

}

javascript reactjs redux redux-thunk react-hooks

4
推荐指数
2
解决办法
7575
查看次数

如何将 MongooseArray.prototype.pull() 与打字稿一起使用?

Typescript 在这一行抱怨:

user.posts.pull(postId);
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

     Property 'pull' does not exist on type 'PostDoc[]'
Run Code Online (Sandbox Code Playgroud)

由于 postId 被接收为req.params.postId字符串类型,所以我将其转换为 mongoose objectId 但我仍然有相同的错误:

  user.posts.pull(mongoose.Types.ObjectId(postId));
Run Code Online (Sandbox Code Playgroud)

pull() 在猫鼬数组中工作。这行代码我是如何在javacsript中实现的。我正在将我的项目转换为打字稿。这是用户模型的用户界面和架构。

interface UserDoc extends mongoose.Document {
  email: string;
  password: string;
  posts: PostDoc[];
  name: string;
  status: string;
}
const userSchema = new Schema({
  email: { type: String, required: true },
  password: { type: String, required: true },
  name: { type: String, required: true },
  status: { type: String, default: "I am a new user" },
  posts: [{ type: Schema.Types.ObjectId, …
Run Code Online (Sandbox Code Playgroud)

javascript mongoose mongodb node.js typescript

4
推荐指数
1
解决办法
1208
查看次数

设置令牌 URI 函数

我了解到 setTokenURI 函数不再使用。如何更改我想要创建的 NFT 代币的代币 URI?现在我的智能合约中的 createCollectible 函数如下所示:

function createCollectible(string memory tokenURI)
    public
    returns (uint256)
{
    uint256 newItemId = tokenId;
    _safeMint(msg.sender, newItemId);
    _setTokenURI(newItemId, tokenURI);
    tokenId = tokenId + 1;
    return newItemId;
}
Run Code Online (Sandbox Code Playgroud)

ethereum solidity smartcontracts nft erc721

4
推荐指数
1
解决办法
5617
查看次数

错误[E0412]:在此范围内找不到类型“ProgramResult”

use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
pub mod myepicproject {
  use super::*;
  pub fn start_stuff_off(ctx: Context<StartStuffOff>) -> ProgramResult {
    Ok(())
  }
}

#[derive(Accounts)]
pub struct StartStuffOff {}
Run Code Online (Sandbox Code Playgroud)

我有上面的源 Rust 代码和下面的错误。

error[E0412]: cannot find type `ProgramResult` in this scope
 --> programs/myepicproject/src/lib.rs:8:58
  |
8 |   pub fn start_stuff_off(ctx: Context<StartStuffOff>) -> ProgramResult {
  |                                                          ^^^^^^^^^^^^^ not found in this scope

For more information about this error, try `rustc --explain E0412`.
error: could not compile `myepicproject` due to previous error
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?

使用锚点

rust blockchain smartcontracts solana anchor-solana

4
推荐指数
2
解决办法
5448
查看次数

即使我切换帐户,Ethers.js 也会返回相同的钱包地址

我使用Ethers.js允许用户将他们的 Metamask 钱包连接到我的应用程序。这是我的代码:

import { ethers } from "ethers"

async function connect() {
    const provider = new ethers.providers.Web3Provider(window.ethereum, "any")
    await provider.send("eth_requestAccounts", [])

    const signer = provider.getSigner()

    const address = await signer.getAddress()

    // Always prints the address that I first connected with
    console.log(address)
}
Run Code Online (Sandbox Code Playgroud)

问题是,一旦我连接了我的一个 Metamask 帐户,即使我切换到另一个 Metamask 帐户并尝试连接它,我也总是会得到它的钱包地址。

这是为什么?我应该如何解决这个问题?

javascript ethereum web3js metamask ethers.js

4
推荐指数
1
解决办法
7705
查看次数

可靠性:内存与堆栈

Solidity有3种不同的内存存储:存储、内存和堆栈。网上看了很多文章,还是不明白内存和栈的区别。我的问题是:

Q1. 内存和堆栈有什么区别?

Q2。假设我在函数中定义了一个局部变量,我怎么知道这个变量是存储在内存中还是栈中呢?(只有当变量声明带有“memory”关键字时,变量才在内存中?)

感谢大家。


感谢@Yilmaz 的回复。根据你的回答,假设我们有一个这样写的函数:

function test() public {
    string memory str;
    int i;
}
Run Code Online (Sandbox Code Playgroud)

str是否i同时在“内存”和“堆栈”上?

我的第三个问题是:

Q3。为什么只有数组、结构体和映射类型需要指定内存位置?为什么 Solidity 不允许我写int memory i;上面的代码?

storage blockchain ethereum solidity smartcontracts

4
推荐指数
1
解决办法
1542
查看次数

如何使用 Remix 仅渲染组件客户端?

我有一个只能在客户端呈现的 Three.js 组件,如何使用 Remix 来实现这一点?Remix 是否公开任何实用程序来帮助确定我们处于服务器还是客户端上下文中?找不到任何东西。

这是我尝试使用混音中不存在的虚构挂钩进行操作的示例。

import React from 'react';
import { useRemixContext } from '@remix-run/react';

const MyComponent = () => {
  const remixContext = useRemixContext();
  if(remixContext.server) {
     return null;
  }
  return <div>Should only be rendered on the client</div>
}
Run Code Online (Sandbox Code Playgroud)

node.js reactjs server-side-rendering remix.run

4
推荐指数
2
解决办法
1万
查看次数