我见过两种在 React 中解构 props 的方法。
function MyComponent({name,age,height}){
// do stuff here
}
Run Code Online (Sandbox Code Playgroud)
或者
function MyComponent(props){
const {name,age,height} = props
// do stuff here
}
Run Code Online (Sandbox Code Playgroud)
假设这个组件被用作
<MyComponent name="bob" age={25} height = {175} haspets={false}/>
这是我的问题:
如果我使用第一种解构方式,是否意味着我将无法访问其他专业人士,在这种情况下 haspets
这两种方式的优缺点是什么?
我正在关注 redux 教程https://redux.js.org/basics/actions并且很难理解以下代码
export function addTodo(text) {
return { type: ADD_TODO, text }
}
Run Code Online (Sandbox Code Playgroud)
所以上面的函数返回对象{ type: ADD_TODO, text },有两件事让我感到困惑。
与 key 关联的该对象的值是多少text。如果这个值是未定义的,那么为什么不直接返回呢{ type: ADD_TODO}。
如果text是字符串,那么不应该是{ type: ADD_TODO, [text]: *some value* }字符串吗?
此外,还有其他功能,例如
function toggleTodo(index) {
return { type: TOGGLE_TODO, index }
}
function setVisibilityFilter(filter) {
return { type: SET_VISIBILITY_FILTER, filter }
}
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释这个语法吗?
我正在尝试实现一个反应流畅的轮播,但我无法将图像垂直居中。这里演示了这个问题。
https://codesandbox.io/s/react-slick-playground-o7dhn
这是问题所在
图像不居中:
justify-content: center;
align-items: center;)margin:auto仅适用于水平对齐(如果我使用 Flexbox,则不必设置)padding:0pxdiv 和margin-top:0px图像上)因此,任何高度为 400px 或更高的图像都会被移动和切断(div 的高度为 400px)我如何解决它。
我已按照建议全局卸载了 create-react-app 。然后我运行npx create-react-app test并出现以下错误
Error: EEXIST: file already exists, mkdir 'C:\Users\User'
TypeError: Cannot read property 'loaded' of undefined
at exit (C:\Program Files\nodejs\node_modules\npm\lib\utils\error-handler.js:97:27)
at errorHandler (C:\Program Files\nodejs\node_modules\npm\lib\utils\error-handler.js:216:3)
at C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js:78:20
at cb (C:\Program Files\nodejs\node_modules\npm\lib\npm.js:225:22)
at C:\Program Files\nodejs\node_modules\npm\lib\npm.js:263:24
at C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:81:7
at Array.forEach (<anonymous>)
at C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:80:13
at f (C:\Program Files\nodejs\node_modules\npm\node_modules\once\once.js:25:25)
at afterExtras (C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:171:20)
C:\Program Files\nodejs\node_modules\npm\lib\utils\error-handler.js:97
var doExit = npm.config.loaded ? npm.config.get('_exit') : true
^
TypeError: Cannot read property 'loaded' of undefined
at exit (C:\Program Files\nodejs\node_modules\npm\lib\utils\error-handler.js:97:27)
at process.errorHandler …Run Code Online (Sandbox Code Playgroud) reactjs ×2
css ×1
html ×1
javascript ×1
npm ×1
npm-install ×1
npx ×1
react-props ×1
react-slick ×1
redux ×1
slick.js ×1