我正在尝试创建一个布局组件来呈现页眉和页脚。我想在 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) 首先抱歉我的英语不好
所以,这是我的代码:
let x = 5
const {x: y=7} = {x}
console.log(y) // output is 5
Run Code Online (Sandbox Code Playgroud)
但为什么是 5?不是7?
我试图通过那里的例子来获取人对象: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) 我在反应中的 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) 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)
我正在尝试从调用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)
我在以下代码序列中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)
我该如何在上方或下方进行此操作?
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)
我定items
要null
和console.log
与null
仍然出现在我试着登录的结果,我得到我的数据Im寻求帮助,请!也试过在es6中使用箭头函数还是不行setState
我正在处理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) 我正在尝试使用对象中的函数,但没有成功。
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)
我有以下代码,我试图从基类继承.为什么代码说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
javascript ×10
reactjs ×6
ecmascript-6 ×2
react-hooks ×2
async-await ×1
asynchronous ×1
axios ×1
class ×1
es6-promise ×1
eslint ×1
extends ×1
fetch ×1
graphql ×1
graphql-php ×1
nodes ×1
object ×1
php ×1
promise ×1
react-router ×1
typescript ×1