小编Har*_*rma的帖子

React Hooks:useEffect的分派动作

我的文件夹结构:

|--App
  |--Components
    |--PageA.js
    |--PageB.js
    |--PageC.js
  |--common-effects
    |--useFetching.js
Run Code Online (Sandbox Code Playgroud)

我正在使用react hooks重构我的代码以从API获取数据。我想调度由saga中间件截获的useFetching.js中的useEffect操作。仅在安装了组件(PageAPageBPageC)时才分派该动作。

我正在使用reduxreact-reduxredux-saga

PageA.js

function(props) {
  useFetching(actionParams)
  //....//
}
Run Code Online (Sandbox Code Playgroud)

PageBPageC组件的类似代码。

我已经抽象了可重用的代码以在useFetching Custom hook中获取数据。

useFetching.js

const useFetching = actionArgs => {
  useEffect( () => {
    store.dispatch(action(actionArgs)); // does not work
  })
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何dispatchuseFetching中访问redux 。我尝试了一下useReducer,但sagas错过了动作。

javascript reactjs redux redux-saga

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

在高阶组件中使用 ref

我有一个Tableref附加到的组件。

使用:Table.js

class Table extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      rows: 1,
      dataLength: props.dataLength,
    }
    this.tableRef = React.createRef(); 
  }

  componentDidUpdate() {
    //using ref
    this.tableRef.current ..... //logic using ref
    this.state.rows ..... //some logic
  }

  render() {
    <TableContainer ref={this.tableRef} />
    <CustomPagination />
  }
}
Run Code Online (Sandbox Code Playgroud)

这工作正常,但现在我的要求发生了变化,我想重用 Table 组件,并将分页应用于我的应用程序中的所有表。我决定做一个 HOC withCustomPagination

使用:withCustomPagination.js HOC

import CustomPagination from 'path/to/file';

const withCustomPagination = tableRef => Component => {
  return class WithCustomPagination extends React.Component {
    constructor(props) {
      super(props); …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

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

转发引用:forwardedRef 始终为空

我有一个Tableref附加到的 ant 设计组件。

我希望能够使用tableRefin HOCwithCustomPagination的生命周期componentDidUpdate方法。

遵循React Docs Forwarding Refs之后,我无法清楚地理解。我可以编写以下代码:

应用程序.js

import WrappedTable from '/path/to/file';

class App extends React.Component {
  render() {
    const tableRef = React.createRef();
    return (
      <WrappedTable ref={tableRef} />
    )
  }
}
Run Code Online (Sandbox Code Playgroud)

表.js

import withCustomPagination from '/path/to/file';

class Table extends React.Component {
  constructor(props) {
    super(props); 
  }

  render() {
    <TableContainer ref={this.props.forwardedRef} />
  }
}

const WrappedTable = withCustomPagination(Table);
export default WrappedTable;
Run Code Online (Sandbox Code Playgroud)

withCustomPagination.js

import CustomPagination from 'path/to/file';

const withCustomPagination …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

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

redux-saga:函数被调用两次

我通过从 saga 分派操作,根据 saga 中间件中执行的某些逻辑的结果来切换 React 中模态的可见性。

我经历了:

店铺

export default function configureStore(preloadedState) {
  const sagaMiddleware = createSagaMiddleware();
  const middlewares = [..otherMiddleware, sagaMiddleware, ...someMoreMiddlewares];

  const store = createStore({
    // other configuration,
    // middleWares
  })

  sagaMiddleware.run(rootRunner);
  return store;
}
Run Code Online (Sandbox Code Playgroud)

减速机

const initialState = {
  activeSwitch: '1',
  modalVisibility: false,
}

export default function reducer(state = initialState, action) {
  switch (action.type) {
    case 'TOGGLE_MODAL':
      return state.set('modalVisibility', !state.get('modalVisibility'));
    case 'UPDATE_ACTIVE_SWITCH':
      // update active switch
    default:
      return …
Run Code Online (Sandbox Code Playgroud)

javascript generator reactjs redux redux-saga

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

安装rails时出错:nokogiri需要Ruby版本<2.4,> = 2.1.0

我试图在Windows 8.1上安装ruby 2.4.1p111后从'使用Ruby启动命令提示符(这就像安装了ruby的命令提示符)'一样安装rails.

我使用该命令,gem install rails并在几秒钟的暂停后,问题的标题被抛出为错误.

我尝试了命令gem install nokogiri -v 1.7.1,它会抛出同样的错误.

如果我跑gem list,它根本不列出nokogiri.

我遇到的一个可能的解决方案.使用一些命令更改gem中的nokogiri版本Gemfilegem 'nokogiri', '~> 1.6.8'.我不知道这是否适用于我安装的rails版本.如果这是解决方案,我该如何实现它?

如何纠正此错误并安装rails?

ruby ruby-on-rails nokogiri

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

Linux 编程:编译带有依赖项的代码

我是 Linux 编程的新手,是从Michael Kerrisk 的 The Linux Programming Interface 中学习的

我必须编译我的第一个具有依赖项的程序。

目录结构:

--linux-programs
  |--seek_io.c
  |--lib
    |--tlpi_hdr.h
    |--error_functions.h
    |--error_functions.c
    |--get_num.h
    |--ename.c.inc
Run Code Online (Sandbox Code Playgroud)

我想编译seek_io.c程序,在lib目录下有依赖项,这样我就可以看到程序是如何工作的。

我尝试了一些东西,对于它们如何在这个stackoverflow 答案之后工作完全一无所知。我遇到了各种各样的错误,因为我是 Linux 编程的绝对初学者,而不是编程、linux OS 和 C。


试炼:

gcc -I ./lib/ -c ./lib/error_functions.c然后gcc -o seek_io.c ./error_function.o给出错误: /usr/lib/gcc/x86_64-linux-gnu/crt1.o: In function _start: (.text+0x20): undefined reference to main collect2: error: ld returned 1 exit status

这次运行后,ls我发现我seek_io.c的没有列出。


基本上这本书的作者对tlpi_hdr.h文件说:

该头文件包括许多示例程序使用的各种其他头文件,定义了布尔数据类型,并定义了用于计算两个数值的最小值和最大值的宏。使用这个头文件可以让我们的示例程序更短一些。


链接到上述文件的代码:

tlpi_hdr.h

error_functions.h

error_functions.c

get_num.h

get_num.c

seek_io.c

c linux gcc linux-kernel

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

从对象数组中提取这些属性

假设我有一个对象数组,这里我假设对象具有三个属性,但可能更多,我想用属性名称提取其中一些:

objArr = [{
  name : "name",
  description : "description",
  date : "date"
},{
  name : "name",
  description : "description",
  date : "date"
},{
  name : "name",
  description : "description",
  date : "date"
}]
Run Code Online (Sandbox Code Playgroud)

name比如说,我只想从上述 中提取 的值objArr。我可以使用以下方法来做到这一点:

(function(objArray){
  objArray.forEach(function(arrObjItem) {
    for(let name in arrObjItem) {
      if(arrObjItem.hasOwnProperty(name)) {
        console.log(objArrItem.name)
      }
    }
  })
})(objArr)
Run Code Online (Sandbox Code Playgroud)

但我真正想要的是提取两个以上属性的值namedescription/或值,如果问题具有不同的数据结构,每个对象有更多属性。最后,我想创建这些提取的属性的映射。(或具有提取的属性、值的新对象数组)(或具有提取的属性、值对的元组)。

javascript data-structures

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