小编fal*_*sky的帖子

ReactJS 标题布局,你不应该在 <Router> 之外使用 <Link>

我正在尝试创建一个布局组件来呈现页眉和页脚。我想在 Header 创建菜单列表,但从 Header.jsx 收到此错误。

Error: You should not use <Link> outside a <Router>
Run Code Online (Sandbox Code Playgroud)

我想知道如何在 Header.jsx 中使用 < Link > 的解决方案

应用程序.jsx

import React from 'react';
import {BrowserRouter as Router,Route,Link,Switch,Redirect,browserHistory} from 'react-router-dom';
    class App extends React.Component {

      render() {
            return (
                <Layout>
                    <Router history={browserHistory}>
                        <div>
                            <Route path="/" component={home}/>
                            <Route path="/docs" component={docs}/>
                        </div>
                    </Router>
                </Layout>
            );
        }


    }

export default App;
Run Code Online (Sandbox Code Playgroud)

布局.jsx

class Layout extends React.Component {
    render() {
        return (
            <div>
                <Header />
                {this.props.children}
                <Footer />
            </div>
        )
    }
} …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-router

3
推荐指数
1
解决办法
7910
查看次数

为什么变量“y”的值为 5?不是7?

首先抱歉我的英语不好

所以,这是我的代码:

let x = 5
const {x: y=7} = {x}

console.log(y) // output is 5
Run Code Online (Sandbox Code Playgroud)

但为什么是 5?不是7?

javascript object destructuring

3
推荐指数
1
解决办法
55
查看次数

php graphQl类型为“ ID!\”的“字段\“ person \”参数\“ id \”是必需的,但未提供。”,

我试图通过那里的例子来获取人对象:https : //github.com/webonyx/graphql-php/tree/master/examples/01-blog

我应该如何调试呢?

IndexController.php

    public function indexAction()
    {
        if (!empty($_GET['debug'])) {
            // Enable additional validation of type configs
            // (disabled by default because it is costly)
            Config::enableValidation();
            // Catch custom errors (to report them in query results if debugging is enabled)
            $phpErrors = [];
            set_error_handler(function($severity, $message, $file, $line) use (&$phpErrors) {
                $phpErrors[] = new ErrorException($message, 0, $severity, $file, $line);
            });
        }
        try {
           /* // Initialize our fake data source
            DataSource::init();*/
            // Prepare context that will be available …
Run Code Online (Sandbox Code Playgroud)

php graphql graphql-php

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

useState 和 splice 反应

我在反应中的 useState 有一个大问题..
我只想使用切片从数组中删除一个对象,并使用钩子设置这个数组。
但它不起作用我不知道为什么..

看:

  const [ img, setImg ] = useState(allImages);

  const removeImageAtIndex = (index) => {
    img.splice(index, 1); // when i console.log i can see that the object has been removed
    setImg(img);         // nothing changes
    /* FOR TEST
    setImg([]); // if I remove all img, it's working 
    */
    /* I already tried this:
    let temp = img;
    img.splice(index, 1);
    setImg(temp);
    */
  /* SOLUTION
    setImg([...img]);
  */
  };

  return (
    <div>
      { variants.map((variant, index) => {
        return <img key={index …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-hooks

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

can't use useState with typescript (Cannot find name 'useState')

What is the problem with below code? I got warning by typescript when use useState

import * as React, { useState } from 'react'

const useForm = (callback: any | undefined) => {
  const [inputs, setInputs] = useState({}) //error: Cannot find name 'useState'.ts(2304)


  const handleInputChange = event => {
    event.persist()
    setInputs(inputs => ({
      ...inputs,
      [event.target.name]: event.target.value,
    }))
  }

  return {
    handleInputChange,
    inputs,
  }
}

export default useForm
Run Code Online (Sandbox Code Playgroud)

https://codesandbox.io/s/react-typescript-starter-lmub8

javascript typescript ecmascript-6 reactjs react-hooks

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

使用异步等待模式重新调整承诺,但有一些延迟

我正在尝试从调用rest api 的方法返回一些测试数据。为了模拟其余 api 调用,我添加了一些延迟以使用异步等待模式返回承诺,但它没有按预期工作。

我对 JavaScript 中异步等待模式的理解是,从异步函数返回的任何值都会作为承诺返回,因此函数应该将值 100 作为承诺返回,因此最后一个语句 using 应该.then显示 100,但事实并非如此。

问题 下面的代码片段有什么问题导致最后一行代码中显示警报而undefined不是 100?

async function f() {

  function delayedResponse() {
    setTimeout(function() { return 100}, 5000);
  }
  return await delayedResponse();

}

f().then(alert); // 100
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous promise async-await es6-promise

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

如何在React中检查对象是否为null,空或未定义?

我在以下代码序列中React

{financialPerformance && (
  financialPerformance.isConfirmed ? (
    <L.Text txtGray>Confirmed</L.Text>
  ) : (
    <L.Text txtGray>Not Confirmed</L.Text>
  )
)}
Run Code Online (Sandbox Code Playgroud)

我也必须检查financialPerformance自身是否为null空或undefined显示“未确认”消息。我的意思是第一次出现financialPerformance物体。

{financialPerformance && (
Run Code Online (Sandbox Code Playgroud)

我该如何在上方或下方进行此操作?

javascript reactjs

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

setState 在 axios fetch 中不起作用

class ItemsPage extends React.Component {

  constructor(props)
  {
    super(props)
    this.state = {items: null }
    this.fetchImages = this.fetchImages.bind(this)
  }


  fetchImages()
  {
    var self = this

    axios.get('https://api.instagram.com/v1/users/self/media/recent/?access_token=')
      .then(function (result)  {
        const items = result
        self.setState({ items: 'test' })
      });

  }

  componentDidMount()
  {
    this.fetchImages()
    console.log(this.state.items)
  }

  render () {

    return (

      <div>Test</div>


    );

  }
}
Run Code Online (Sandbox Code Playgroud)

我定itemsnullconsole.lognull仍然出现在我试着登录的结果,我得到我的数据Im寻求帮助,请!也试过在es6中使用箭头函数还是不行setState

javascript fetch nodes reactjs axios

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

未定义错误“propTypes”且未定义错误“defaultProps”

我正在处理react-redux应用程序的一些可访问性问题。

我安装了npm install eslint eslint-plugin-jsx-a11y@4.0.0 --save-dev,现在当我运行我的应用程序时,出现以下错误:

Failed to compile.

Error in ./src/components/Promo.jsx

/code/src/components/Promo.jsx
  15:10  error  'propTypes' is not defined       no-undef
  19:10  error  'defaultProps' is not defined    no-undef
  42:3   error  'updateCellCode' is not defined  no-undef

? 3 problems (3 errors, 0 warnings)
Run Code Online (Sandbox Code Playgroud)

我没有看到任何语法错误,代码对我来说看起来很正确:

import React, { PureComponent, PropTypes } from 'react';
import { throttle, prepAndFormatForInnerHTML } from '../util';
import '../assets/styles/application/promo.css';
import Promos from '../assets/strings/promos.json'; // placeholder info

const MOBILE_BREAKPOINT = 679; // from scss file

/** Used …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs eslint react-proptypes

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

如何在不使用 jquery 的情况下在纯 JavaScript 中使用对象的函数?

我正在尝试使用对象中的函数,但没有成功。

let ops = [
    {'*': (a, b) => a * b}, 
    {'/': (a, b) => a / b},
    {'+': (a, b) => a + b},
    {'-': (a, b) => a - b}
];

let res = [];
let current;

for (var i = 0; i < ops.length; i++) {
   current = ops[i];
   res = current(4, 2);
   console.log(res);
}
Run Code Online (Sandbox Code Playgroud)

javascript

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

javascript:调用基类函数

我有以下代码,我试图从基类继承.为什么代码说identify()没有定义?它不应该从基类调用函数吗?

错误:ReferenceError:识别未定义source1.js:23:9

class TestBase {
    constructor() {
        this.type  = "TestBase";
    }

    run() {
        console.log("TestBase Run");
    }

    identify() {
        console.log("Identify:" + this.type);
    }
}

class DerivedBase extends TestBase {
    constructor() {
        super();
        this.type  = "DerivedBase";
    }

    run() {
        console.log("DerivedBase Run");
        identify();
    }
}

window.onload = function() {
  let derived = new DerivedBase();
  derived.run();
}
Run Code Online (Sandbox Code Playgroud)

javascript extends class prototypal-inheritance ecmascript-6

0
推荐指数
1
解决办法
204
查看次数