Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 反应上下文重置使用者状态_Javascript_Reactjs_Context Api - Fatal编程技术网

Javascript 反应上下文重置使用者状态

Javascript 反应上下文重置使用者状态,javascript,reactjs,context-api,Javascript,Reactjs,Context Api,我试图使用React上下文API将状态数据从父组件传递到深度嵌套的子组件 父级是类组件,子级是函数 当我使用通过上下文传递的函数从子级更新父级状态时,父级状态将成功更新,但子级的本地状态将重置为初始值 家长: export class ObjectLinking extends Component { constructor(props) { super(props); this.state = {

我试图使用React上下文API将状态数据从父组件传递到深度嵌套的子组件

父级是类组件,子级是函数

当我使用通过上下文传递的函数从子级更新父级状态时,父级状态将成功更新,但子级的本地状态将重置为初始值

家长:

 export class ObjectLinking extends Component {
        constructor(props) {
            super(props);
            this.state = {
                setCurrentlyDisplayed: this.setCurrentlyDisplayed,
                currentlyDisplayed: []
            };
        }

        [...]

        render() {
            return (
                <ObjectLinkingContext.Provider value={this.state}>
{//Panel body will contain deep nested child that is connected to context}
                   <PanelBody />
                </ObjectLinkingContext.Provider>
            );
        }
    }
导出类ObjectLinking扩展组件{
建造师(道具){
超级(道具);
此.state={
setCurrentlyDisplayed:this.setCurrentlyDisplayed,
当前显示:[]
};
}
[...]
render(){
返回(
{//面板正文将包含连接到上下文的深嵌套子级}
);
}
}
儿童:

  import ObjectLinkingContext from '../../context/ObjectLinkingContext';
    const AssetListFilters = ({ assets, filtersModel }) => {
        const [searchByNameVal, setSearchByNameVal] = useState([]);
        const panelContext = useContext(ObjectLinkingContext);

        useEffect(() => {
            filterBySearchNameVal();
        }, [searchByNameVal]);

        const filterBySearchNameVal = () => {
            if (searchByNameVal.length) {
                const { setCurrentlyDisplayed } = panelContext;
                const { value: searchedId } = searchByNameVal[0];
                const searchedAsset = assets.filter(asset => asset.assetId === searchedId) || [];
                setCurrentlyDisplayed(searchedAsset);
            }
        };

        return (
                        <Autocomplete
                            onChange={val => setSearchByNameVal(val)}
                        />

      );
    };
从“../../context/ObjectLinkingContext”导入ObjectLinkingContext;
const AssetListFilters=({assets,filtersModel})=>{
const[searchByNameVal,setSearchByNameVal]=useState([]);
const panelContext=useContext(ObjectLinkingContext);
useffect(()=>{
filterBySearchNameVal();
},[searchByNameVal]);
const filterBySearchNameVal=()=>{
if(searchByNameVal.length){
const{setCurrentlyDisplayed}=panelContext;
const{value:searchedId}=searchByNameVal[0];
const searchedAsset=assets.filter(asset=>asset.assetId===searchedId)| |[];
设置当前显示(搜索资产);
}
};
返回(

完整的Parent.js和VeryDeepChild.js组件

你能发布相关的代码吗?请注意,相关是重点。发布一个指向近300行代码的链接,基本上是问“怎么了?”不是您应该如何发布问题。您在子项中有一个
useffect
,这意味着您正在调用
setcurrentlydisplay(searchedAsset)
在初始渲染时。每当更新上下文状态时,使用该上下文的所有子级都会重新渲染。这是您的意图吗?@JMadelaine在初始渲染时
searchByNameVal。长度
0
因此不会调用
setCurrentlyDisplayed
。我正在使用useffect,因为我想将其称为
setCurrentlyD>当更新
searchByNameVal
时显示
isplayed如果希望父状态与子状态匹配,为什么还要有子状态?只需在父状态中跟踪该状态即可