小编Dav*_*888的帖子

setState 不适用于更新 React 中的数组

我尝试更新state位置对象中的 a 。不知何故 setState 对我不起作用。

console.log不正确的返回了newName。

我没有直接看出我的错在哪里。有人能告诉我我的错误在哪里吗?

state = {
    locations: [
       {name:"name1", address:"address1", locationSelected:false},
       {name:"name2", address:"address2", locationSelected:false}
    ]
}

selectLocationHandler = (id) => {
    let theLocations = [...this.state.locations];
    theLocations[id] = {...theLocations[id], name:"newName!!!!"};
    console.log(theLocations[id].name + "testtest");

    this.setState({theLocations}, () => {
        console.log(this.state.locations[id].name + " it worksss");
    });
}
Run Code Online (Sandbox Code Playgroud)

reactjs

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

在进行反应之前获取数据的最佳位置

我想知道在生命周期中应该从哪里获取数据。我试图将数据放入componentDidMount()componenWillMount() 但未成功...

componentWillMount(){

 // this fetch the data from back-end set a store state with the payload
        this.props.fetchUser(); 
        this.setState({userData:this.props.auth});
}

//fetchMethod
export const fetchUser = () => async dispatch =>{//using redux-thunk


    const res= await axios.get('/api/current_user')

    dispatch({type:FETCH_USER, payload:res.data}); 



};
Run Code Online (Sandbox Code Playgroud)

在我的渲染函数中,我尝试通过调用使用提取的userData this.state.userData。但这是不确定的。我也尝试通过调用正确的存储状态来获得它,也没有成功。根据我的本地存储,我没有得到的是定义了存储状态。希望有人能告诉我我做错了什么。谢谢!

reactjs redux

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

如何在react.js中将参数传递给API get请求

我想知道有没有办法从客户端向后端API get Request传递一个参数。此时,我对所需的参数进行了硬编码(名称:“newName”)。

后端路由:

app.get('/api/get/beerWithComments', (req,res,next) =>{



    Beer.findOne({name:'newName'}) //I want to pass the correct name, now it's hard coded.
        .populate('comments').exec((err,beer) =>{
            if(err) return next(err);
            res.json(beer);
        });

});
Run Code Online (Sandbox Code Playgroud)

我的行动方法:

  export const fetchBeerWithComments =() => async dispatch => {
    const res= await axios.get('/api/get/beerWithComments');
    dispatch({type: FETCH_BEER_WITH_COMMENTS, payload : res.data });

}
Run Code Online (Sandbox Code Playgroud)

我想将参数传递到这里。但我不知道是否可以将参数传递到我的后端。

export const fetchBeerWithComments =(parameter) => async dispatch => {...
Run Code Online (Sandbox Code Playgroud)

mongoose node.js express reactjs

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

控制器中的NullReferenceException c#

NullReferenceException试着运行一个视图.我没有看到导致此问题的代码.有人可以解释这个问题吗?谢谢

这是Model类:

public class Catalogus: ICatalogus
{
    private readonly DbSet<Materiaal> materialen;
    private IEnumerable<Materiaal> materialenTest;
    private Firma firma;

    public Catalogus()
    {
        firma = new Firma("hh", "lol@gmail.com");
        materialenTest = new Materiaal[] { new Materiaal(5, 0, "1", "test", "test", "ts", firma, "wereldbol", "wereldbol", "lol", 0, true) };
    }

    public IEnumerable<Materiaal> VindAlleMaterialen()
    {
        return materialenTest.OrderBy(m => m.Naam);
    }

    public IEnumerable<Materiaal> ZoekOpTrefwoord(string trefwoord)
    {
        IEnumerable<Materiaal> gefilterdMaterialen = materialenTest.Where(mat => mat.GetType().GetProperty("naam").GetValue(this).Equals(trefwoord));

        return gefilterdMaterialen;
    }
} 
Run Code Online (Sandbox Code Playgroud)

具有NullRef异常的控制器:

这条线导致了这个问题.

IEnumerable materialen = catalogus.VindAlleMaterialen().OrderBy(m => m.Naam).ToList();

public …
Run Code Online (Sandbox Code Playgroud)

.net c# nullreferenceexception

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

标签 统计

reactjs ×3

.net ×1

c# ×1

express ×1

mongoose ×1

node.js ×1

nullreferenceexception ×1

redux ×1