小编cdm*_*dmt的帖子

React Typescript - 类型参数不可分配给类型参数

这里有一个演示

这是一个 React 应用程序,使用 Typescript 和钩子将条目捕获到下面简单显示的表单中。

在 Stackblitz 中它可以工作,但在本地我使用的是 VS 代码,我收到一个错误,setUser(userData);因为错误是

const userData: {
    username: string;
    password: string;
    prevState: null;
}
Argument of type '{ username: string; password: string; prevState: null; }' is not assignable to parameter of type '(prevState: null) => null'.
  Type '{ username: string; password: string; prevState: null; }' provides no match for the signature '(prevState: null): null'.ts(2345)
Run Code Online (Sandbox Code Playgroud)

我怎么能解决这个打字稿错误

typescript reactjs

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

Git 基础 - 将旧分支合并到 master 中

我正在开发一个项目,该项目有一个旧分支“新变化”。

这个分支是几周前创建的,现在需要将其添加到 master 中

自从“new-changes”分支创建以来,master 中发生了很多变化。

我现在需要将“new-changes”中的更改合并到 master 中。

如果我在大师那里

git merge new-changes
Run Code Online (Sandbox Code Playgroud)

它只是添加“new-changes”中的内容还是会将 master 恢复到创建“new-changes”时的状态。

将“new-changes”中的更改添加回 master 的最佳方法是什么

git git-merge

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

React、Typescript、Hooks - 类型上不存在属性“状态”

我正在尝试使用 React、Typescript 和钩子 useContext 和 useReducer 创建一个简单的待办事项应用程序。

索引.tsx

import React, { useContext, useReducer } from "react";
import ReactDOM from "react-dom";
import "./index.css";

import TodosContext from "./context";
import todosReducer from "./reducer";
import TodoList from "./components/TodoList";
import { ITodo } from "./context";

const App = () => {
  const initialState = useContext(TodosContext);
  const [state, dispatch] = useReducer(todosReducer, initialState);

  return (
    <TodosContext.Provider value={{ state, dispatch }}> <!-- error here --->
      <TodoList />
    </TodosContext.Provider>
  );
};

ReactDOM.render(
  <App />,

  document.getElementById("root")
);
Run Code Online (Sandbox Code Playgroud)

这里我得到状态错误

Type '{ …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs react-hooks

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

样式化组件 - 使用样式化组件作为另一个组件的基础

我认为这可以通过样式化组件实现

使用第一个样式组件Block作为另一个组件的基础,例如

export const BlockOne = styled.Block

import React, { Component } from 'react';
import { render } from 'react-dom';
import './style.css';
import styled from 'styled-components'

export const Block = styled.div`
  width: 100px;
  height: 100px;
  background: red;
`

export const BlockOne = styled.Block`
  background: yellow;
`

const App = () => {
  return (
    <div>
      <Block/>
      <BlockOne/>
    </div>
  );
}

render(<App />, document.getElementById('root'));
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点

reactjs styled-components

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

.toBe 不是函数

我创建了一个反应打字稿应用程序npx create-react-app . --template typescript

我添加了一个基本测试文件

import '@testing-library/jest-dom/extend-expect'

const sumPositiveNumbers = (num1:number, num2: number) => {
    return num1 + num2
}

describe('when test pass', () => {

    test('should return an answer', () => {
        expect(sumPositiveNumbers(4,5).toBe(9))
    })

})
Run Code Online (Sandbox Code Playgroud)

我收到错误

TypeError: sumPositiveNumbers(...).toBe is not a function
Run Code Online (Sandbox Code Playgroud)

package.json 看起来像

{
  "name": "test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "@types/jest": "^26.0.15",
    "@types/node": "^12.0.0",
    "@types/react": "^16.9.53",
    "@types/react-dom": "^16.9.8",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.1",
    "typescript": "^4.0.3",
    "web-vitals": "^0.2.4" …
Run Code Online (Sandbox Code Playgroud)

reactjs jestjs react-testing-library

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