小编gpb*_*lio的帖子

如何使用react router dom v4配置webpack dev服务器?

这是我的webpack配置的代码:

const compiler = webpack({
  entry: ['whatwg-fetch', path.resolve(__dirname, 'js', 'app.js')], 
  module: {
    loaders: [
      {
        exclude: /node_modules/, 
        test: /\.js$/, 
        loader: 'babel',
      },
    ],
  },
  output: {filename: 'app.js', path: '/'}, 
});

const app = new WebpackDevServer(compiler, {
  contentBase: '/public/', 
  proxy: {'/graphql': `http://localhost:${GRAPHQL_PORT}`},
  publicPath: '/js/',
  stats: {colors: true}, 
});

app.use('/', express.static(path.resolve(__dirname, 'public')));
Run Code Online (Sandbox Code Playgroud)

工作正常,应用程序运行在localhost:3000/index.html但是当我尝试实现React Router dom v4时.它不起作用.这是代码:

const About = () => <div> <h1>About</h1> </div>

ReactDOM.render(
   <BrowserRouter>
      <div>
      <Route exact path='/' component={App}/>
      <Route  path='/about' component={About}/>
      </div>
    </BrowserRouter>,
  mountNode
);
Run Code Online (Sandbox Code Playgroud)

这是localhost的结果:3000/build.min.js 在localhost:3000 /约.我收到一个错误:无法获取/关于.不是我期待的,为什么这不会呈现?

关于

reactjs webpack react-router webpack-dev-server react-router-v4

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

如何通过按钮单击反应路由器v4在路径上导航?

我在react-router-dom中有这个路径:

 <BrowserRouter>
  <div>
  <Route exact path='/(index.html)?' component={Home}/> {/* app = home */}
  <Route  path='/register' component={Registration}/>
  </div>
</BrowserRouter>
Run Code Online (Sandbox Code Playgroud)

一切都工作正常,现在我的组件中的任何地方我想通过onClick更改路径,这样的代码:

<button onClick={() => history.push('/the/path') }> change path </button>
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

javascript reactjs react-router-v4 react-router-dom

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

如何将父状态传递给其子组件?

我是React ES6的新手,我想我正在以错误的方式修改状态.当我在父组件上设置状态时,我的代码是这样的:

class App extends React.Component  {
constuctor(props)  {
     super(props);
     this.state = {name:"helloworld"};
}
 render() { 
  return( 
   <ChildComponent parentObj={this} /> // parentObj for parent context as props on child components  
  ); 
 } 
}
Run Code Online (Sandbox Code Playgroud)

我的问题是在其他子组件中,我必须重复这样做,还有另一种方法吗?我没有React.createClass的问题,但我想在es6中编码所以我有这个问题.

ecmascript-6 reactjs

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

隐藏在 react native 上的可见性不起作用,即使没有显示也要占用空间?

我在 flex 容器之间的空间中有 TouchableOpacity,即使没有显示,我也想占用空间,

我的代码:

<TouchableOpacity
  style={showClear && { visibility: 'hidden' }}
  onPress={() => this.props.clearCompleted()}>
  <Text>Clear Completed</Text>
</TouchableOpacity>
Run Code Online (Sandbox Code Playgroud)

display: none 有效但不占用空间,上面的代码无效但在 web 中有效?

react-native

10
推荐指数
3
解决办法
2万
查看次数

jest 配置在更新到 26.x 后抛出“type ErrorHandler = (error: mixed, isFatal: boolean) =&gt; void”

我不知道为什么这突然不起作用,但这是我的 jest.config.js:

module.exports = {
  preset: 'react-native',
  verbose: true,
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  setupFiles: ['./jestSetup.js'],
  transformIgnorePatterns: [
    'node_modules/(?!(jest-)?react-native|@react-native-community|@react-navigation)',
  ],
  moduleNameMapper: {
    '.+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
  },
};
Run Code Online (Sandbox Code Playgroud)

和我的 jestSetup.js:

import 'react-native-gesture-handler/jestSetup';
import '@testing-library/jest-native/extend-expect';
import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock';

beforeAll(() => {
  //@ts-ignore
  global.__reanimatedWorkletInit = jest.fn();
});

jest.mock('react-native-share', () => ({
  default: jest.fn(),
}));

jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage);

jest.mock('react-native-reanimated', () => {
  const Reanimated = require('react-native-reanimated/mock');

  Reanimated.default.call = () => {};

  Reanimated.useSharedValue = jest.fn;
  Reanimated.useAnimatedStyle = jest.fn;
  return Reanimated;
});

jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper'); …
Run Code Online (Sandbox Code Playgroud)

jestjs react-native babel-jest

8
推荐指数
2
解决办法
2876
查看次数

if块是否在函数范围内创建新的局部范围?

例如:

function example() {
   console.log("outside the if block above function b declaration"+b()); 
  function a() {
   return "you invoked function a";
  } 
  if (true) {
    console.log("inside the if block"+a());
    console.log("inside the if block above function b declaration"+b());
    function b() {
      return "you invoked function b";
    } 
  }
}
Run Code Online (Sandbox Code Playgroud)

当我调用这个example()函数时,我得到一个错误,b是未定义的,但当我删除第二行调用函数b在if块中定义时它都可以吗?

javascript

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

如何在反应路由器dom v4中获取组件中的参数?

我有这个代码:

<BrowserRouter>
   <Route  path="/(:filter)?" component={App} />
</BrowserRouter>
Run Code Online (Sandbox Code Playgroud)

根据以前的路由器版本,过滤器参数或"根"假设是在App组件的道具上吗?

这是我在我的应用程序上的代码:

const App = ({params}) => {
    return ( // destructure params on props
      <div>
        <AddTodo />
        <VisibleTodoList 
            filter={params.filter || 'all'} // i want to git filter param or assign 'all' on root 
        />
        <Footer />
      </div>
    );
}
Run Code Online (Sandbox Code Playgroud)

我在控制台上记录了this.props.match.params,但它没有?救命?

javascript reactjs react-router react-router-v4 react-router-dom

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

如何禁用wtform提交字段的提交按钮?

这是 wtform 的提交按钮代码:

{{ form.submit(class="w-50 btn btn-primary btn-sm submit-btn") }}
Run Code Online (Sandbox Code Playgroud)

我正在考虑在单击时向其添加一个类?我知道通过添加这样的类来显示 wtform+jinja2 中的错误的方法:

{% if reg_form.confirm_password.errors %}
   {{ reg_form.confirm_password(class="form-control form-control-sm is-invalid") }}
          <div class="invalid-feedback">
             {% for error in reg_form.confirm_password.errors %}
                <span>{{ error }}</span>
             {% endfor %}
          </div>
   {% else %}
       {{ reg_form.confirm_password(class="form-control form-control-sm")}}
   {% endif %}
Run Code Online (Sandbox Code Playgroud)

我查看了 SubmitField 的文档,但我看不到禁用它的方法。https://wtforms.readthedocs.io/en/stable/fields.html

jinja2 flask flask-wtforms

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

使用 updateMany 返回猫鼬中更新的模型?

这是我的代码:

const result = await Todo.updateMany(
      { _id: { $in: ids }, userId },
      { $set: { complete } },
      { new: true }
    ).populate('userId', '_id');
res.json({ todos: result })
Run Code Online (Sandbox Code Playgroud)

它适用于更新数据,但返回以下内容:

{ n: 1,
  nModified: 1,
   opTime:
    { ts:
       Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1543568180 },
      t: 1 },
   electionId: 7fffffff0000000000000001,
   ok: 1,
   operationTime:
    Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1543568180 },
   '$clusterTime':
    { clusterTime:
       Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1543568180 },
      signature: { hash: …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb

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

在axis.tickFormat() 中使用d3.timeFormat 时出现TypeScript 错误

此代码适用于 JavaScript:

var timeFormat = d3.timeFormat("%M:%S");
var yAxis = d3.axisLeft(y).tickFormat(timeFormat)
Run Code Online (Sandbox Code Playgroud)

但是 TypeScript 中的这段代码不起作用:

const yAxis = d3.axisLeft(y).tickFormat(d3.timeFormat("%M:%S"));
Run Code Online (Sandbox Code Playgroud)

function timeFormat(specifier: string): (date: Date) => string 返回给定字符串说明符的新格式化程序。返回的函数格式化指定的日期,返回相应的字符串。

默认语言环境中 locale.format (TimeLocaleObject.format) 的别名。

@param 说明符 — 日期格式的说明符字符串。

错误是

'(date: Date) => string' 类型的参数不能分配给 'null'.ts(2345) 类型的参数

d3.js typescript

6
推荐指数
2
解决办法
1381
查看次数